I am using libgdx to create a game for android. If I want to set the background as an image that fills up the whole screen, how can I keep the aspect ratio of the image? I don't want remaining blank space or letter boxing, but if you set the image size as the whole screen, it is going to stretch on some devices and it will look distorted...
Related
I have an image which has height greater than its width. I want to achieve something like user can pinch zoom out(or scale out) to fit the whole image inside a cropping window having a fixed aspect ratio (for eg: 16:9). If there is any empty space after zooming out then it should be filled with a default colour like black or white. It should work just like nocrop app for Instagram does, but here the cropped image won't be a square image. I am open to using any android libraries also if it can fix the issue.
Something like
however, i have a weird issue, when drawing, it seems the outside 1px of an image is stretched to fit a rectangle, but the inside is only stetched to an extend, i was drawing to 48x48 tiles, but drew a 500x500 tile to show the issue. [ 500x500 draws fine ]
the worst part seems to be, it chooses when to stretch and not to stretch. and also what to strech. im sorry this is hard to explain but i have attached a image that i hope does a better job.
it could just be misunderstanding how to use a draw with spritebatch
edit: Tile is 48x48 not 64x64, ive just been working all day.
This is because you are not rendering "pixel perfect" which means your image does not line up with the pixel grid of your monitor. A quick fix might be to set a linear filter for your textures, since by default it uses nearest and thus a pixel on the screen will inherit the closest color it can get. A linear filter will interpolate colors and make that line "look" thinner.
texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
If you are using texturepacker you can do this in one go by altering it's settings.
texturePackerSetting.filterMin = Texture.TextureFilter.Linear;
texturePackerSetting.filterMag = Texture.TextureFilter.Linear;
Or you could edit the atlas file itself by by changing the filter parameter to:
filter: Linear,Linear
This obviously costs more power since it needs to do more calculations for each pixel you drawn to the screen but I would not worry about this until your drawing is starting to get a bottleneck.
Another solutions is to draw pixel perfect which means you need to set your viewport to the size of the device gdx.graphics.getWidth, gdx.graphics.getHeight, in other words a ScreenViewport and draw your textures at exact sizes you want them. Of course this means a screen with more pixels sees more of your game world then a screen with less pixels and the more pixels a device has the smaller your textures will look. Another drawback of this is that you have to forget about any zooming or draw sprites for each level of zoom so they line up with the pixel grid of the device again.
I have recently started learning libGDX and currently, I'm making an endless space game. I have made a scrolling background, where there is a base image of space overlapped with another image with stars.
Looks like this. (the black area is transparent)
Stars move a bit faster, what creates nice effect, as if they were closer. It works fine on my mobile phone and tablet, however if I try it on another tablet with slightly different resolution, the whole stars image becomes black and covers the whole space image.
Everything is saved in PNG and I move the sprite by changing it's Y.
stars = new Sprite(new Texture(Gdx.files.internal("data/universe/stars1.png")));
stars.setSize(screenWidth, screenHeight);
stars.setY(0);
Thanks for any advice.
Developing an app for Android and I'm required to have a fullscreen image rotate (spin around infinitely, pivoted at the centre of the screen) in the background.
I have tried rotating the ImageView using RotateAnimation which works well, except the image is cropped to the size of the parent view.
I have also tried expanding the parent view to a set size (750dp) but it doesn't work well across all the different screen sizes.
Does anyone know of an easy way to implement this, which would involve scaling to uniformly fit the screen size?
Cheers,
Dylan
Screenshot of the image:
I am trying to build a custom camera view which fills the whole screen by stretching the preview while maintaining the aspect ratio and having the parts outside the screen cropped. I have been unable to get the surface view which the camera uses to stretch larger than the screen size. I can get it to be smaller than the screen, or have it stretched to match the screen exactly which ruins the aspect ratio. How do I go about getting the camera to fill the screen without losing the aspect ratio?