Welcome.
I need to prepare UI for different screen sizes. I make layouts
layout-small
layout-normal
layout-large
layout-XLARGE
For this I-small drawable, drawable-normal, drawable-large, drawable-XLARGE.
But where to get the screen size from small to XLARGE. I mean the size of
For example, 320 x 480 Where to get the dimensions for each screen? To prepare the graphics?
if you want to do in code you can use Display class of android and use method getRectSize and getSize for the same. Please See API here
If you want to see the differences between screen sizes you can look at this Supporting Multiple Screens
first of all you have to read this at least. As bit of advice, you can start with the graphics for the mdpi device. For the mdpi devices 1 pixel is equivalent to 1 dpi. Then you can make the same graphic for the hdpi just multiply the pixel of the mdpi image * 1.5, and for the ldpi just multiply the pixel of the mdpi image * 0.75..
Related
So imagine that I have an image that is 700x700 px in res folder and I want my app to support different pixel densities. So according to the documentation:
ldpi -- 0.75x
mdpi -- 1x
hdpi -- 1.5x
xhdpi -- 2.0x
xxhdpi -- 3.0x
xxxhdpi -- 4.0x
So now, my question is, if my image is 700x700 px , I can think that those 700x700px are the resolution of 1x (mdpi), so the resolutions for the different densities would be the following:
ldpi -- 525x525px
mdpi -- 700x700px
hdpi -- 1050x1050px
xhdpi -- 1400x1400px
xxhdpi -- 2100x2100px
xxxhdpi -- 2800x2800px
But also I can think that my 700x700 image that I have is for the biggest density, so it is the 4.0x (xxxhdpi) , so the resolutions for the different densities would be the following:
ldpi -- 131.25x131.25px
mdpi -- 175x175px
hdpi -- 262.5x262.5px
xhdpi -- 350x350px
xxhdpi -- 525x525px
xxxhdpi -- 700x700px
But also , I can think that those 700x700px are for ldpi,hdpi,xhdpi,xxhdpi, so there are there are six different possible combinations... I would like to know which of all should be the density base of my 700x700 px image. I think that maybe knowing a pixel range table of all the densities that shows the minimum and maximum pixels for each density would be really helpful.
You could count these values using methods like:
my solution is to get any layout form your .xml by id and just count it using the same methods
layout.width
layout.height
or
// on below line we are creating and
// initializing variable for display metrics.
val displayMetrics = DisplayMetrics()
// on below line we are getting metrics
// for display using window manager.
windowManager.defaultDisplay.getMetrics(displayMetrics)
// on below line we are getting height
// and width using display metrics.
val height = displayMetrics.heightPixels
val width = displayMetrics.widthPixels
Also if you want to handle showing images on UI just use Glide Library
I have been trying for the past hours to find a solution to this problem, but I can't seem to find anything.
I am developing a game for Android using LibGDX. In the emulator, the game looks fine, but when I play it on my phone, everything is different and misplaced. The solution I found for this is using Density Individual Pixels instead of regular pixels, so everything is placed corectly, no matter what device I use. However, I can't seem to find a proper way to do that. The only relevant solution I have found was to use this:
public static float pixelToDP(float dp) {
return dp * Gdx.graphics.getDensity();
}
I tried resizing some of the objects using the formula above, but they are still different from the emulator.
Please, if anyone has a solution that doesn't involve changing the Ortographic Camera(already tried those), help me!
This answer is just to add to the what TomGrill said in the comments.
The reason your game looks fine in the emulator is because you have used values that fits the resolution of the emulator.
If you position a sprite at 100,100 on a 1920x1080 resolution, the sprite will be in the upper (or lower, depending on how you orient your y axis) left corner.
On a 200,200 resolution, the sprite will be placed in the middle of the screen.
The size of the sprite is also dependent on the resolution / pixel density. If you have 1 pixel per sq inch, a 32x32 pixel sprite will be 32 inches wide and high. But on a screen with a high pixel density, lets say 100px pr. sq. inch. the 32x32 sprite will look pretty small.
This is where viewports come in. You choose a resolution, lets say 900x540 and you just code for this resolution. The viewport will make sure your game scales up or down to fit any resolution and pixel density. If you place a sprite in the middle of you 900x540 screen, the viewport will make sure that it is placed in the middle of a 1920x1080 resolution.
Even if you wanted to do these calculations yourself, Gdx.graphics.getDensity(); is not of any use on its own. You need the width and the height of the physical screen to find the resolution. And what you would be doing next is reinventing the wheel.
I'm new to LIBGDX game development, and I have faced my first problem. I've created 9.patch drawables (buttons) using texture packer. This drawables can be used on low density and also extra high density screens and quality is the same.
If I run my project with that drawable on desktop project the image shown is okay and perfect size. If I run project on low density android device, drawable becomes huge (almost half of the screen). And also If I run project on extra high density android device the button becomes really small.
So my question is, how to handle drawables in LIBGDX, so the ratio (screen:image size), stays the same no matter resolution/density..?
If your button is a text button. Change the font of your text.
If you are using image button, this might help you
It kind of depends on what you're drawing...If you are just drawing an image, I've found it easier to specify a float width and height in the last 2 parameters of the draw method. If you are using a camera with a fixed viewport size, you can simply use a fixed percentage of your camera viewport so it will always be the same dimensions and draw like this:
batch.draw(drawable,x,y,screenwidth * 0.5f,screenheight*0.5f);
However, if you are using buttons or some other widget inside of a table, you should specify the cell size and it should automatically be resized based on the size of the table cell. Something like:
myTable.add(myWidget).width(300).height(200);
Post up exactly what it is that you need to draw if you get a chance and it will be easier to figure out what needs to happen.
I have a lot of images with xhdpi resolution. Now I need the same images but in mdpi, ldpi, hdpi resoulution. I think this process could be automated. So, what is the best way to generate images with different resolution?
Try using:
http://android-ui-utils.googlecode.com/hg/asset-studio/dist/index.html
This helps a lot in generating resources. Hope it helps!
The process is already automated. If you provide just one size - hdpi works for me - then the Android system will resize or rescale it as needed for other screen resolutions and sizes.
The drawback of not providing images in different sizes is that the automatic resizing algorithm might not do as good a job as a graphic designer. However, if you are looking for a non Android algorithm to do the resizing, who's to say it will be any better than the built-in algorithm?
I suggest trying just one size on various devices and see how your graphics looks. Some images will scale better than others, so you may only need to provide multiples for a few.
Not too long ago I needed to resize a Image in java and I found a very good method for that, it returns a type of bufferedimage and you will have to use that new instance. You can specicify your width and height. Here is the code:
public BufferedImage resizeImage(final Image image, int width, int height) {
final BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
final Graphics2D graphics2D = bufferedImage.createGraphics();
graphics2D.setComposite(AlphaComposite.Src);
graphics2D.drawImage(image, 0, 0, width, height, null);
graphics2D.dispose();
return bufferedImage;
}
Just putting in 1 image at a higher resolution (like xhdpi or hdpi) and using that for all scales is not efficient as while android does resize it to fit, it still is loading the image in its original size into memory, thus this would cause some lag or outOfMemory errors if you have lot of them in 1 activity.
I would use the bitmap Image loader that google recommends.
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#decodeSampledBitmapFromResource
This lets you take an image, and it will resize it, while only loading the new image size into memory. This is also the method you would want to do if you want a "lazy load".
The solution I chose to handle all my drawables is the following:
1- Have all drawables in high resolution in a /etc folder in your app project.
For the high resolution I chose 10 times the default resolution (mdpi). So for instance the launcher icon drawables (48dip x 48dip which is 48 x 48 pixels in mdpi) would be 480 x 480 pixels.
I have all my drawables in high res in the /etc folder.
2- At build time, my ant script would take all drawables from /etc, and resize them to fill all /res/drawables-* folders.
From the highres drawable at 100% size, the various resolutions sizes will be: ldpi: 7.5%, mdpi: 10%, hdpi: 15%, xhdpi: 20%, xxhdpi: 30%
I use an external program launched by ant to actually do the resizing.
Alright, so I have an image that it's resolution is: 1054X1054.
I want to set that image so it fits exactly the screen size of the android devices. (I'll cut the image in some editor as nessacary).
So my question is: How can I know what resolution my image should be so it will cover the device background without needing to resize the image.(For the mdpi,hdpi,ldpi)
I'm asking that question because I keep missunderstanding how the "multiple screen resolutions" really work..
you can design your image to re-size with different screen sizes, you'll discover that each design requires a minimum amount of space. So, each generalized screen size above has an associated minimum resolution that's defined by the system.
android os count sizes in "dp" units—the same units you should use when defining your layouts—which allows the system to avoid worrying about changes in screen density.
xlarge screens image sizes are at least 960dp x 720dp
large screens image sizes are at least 640dp x 480dp
normal screens image sizes are at least 470dp x 320dp
small screens image sizes are at least 426dp x 320dp