Relationship between image resources and their resultant size on screen - android

Say I have a cat.png in my drawable-hdpi folder, with dimension 100*100 px. Now if I load this picture using an ImageView whose size is set to "wrap_content":
cat_image.setImageResource(R.drawable.cat);
How can I know the size of the image view (in pixels) without calling "getHeight()"? There must be a relationship between the size of image view and the number "100", but I tried to convert "100" from DP to PX, and from PX to DP, etc, with no success.

if a image is 100px that in mdpi is 100dp.
Then something that is 100dp means:
its 100px on mdpi
its 150px on hdpi
its 200px on xhdpi
So android will try to find the best suited resource to use. for drawables it also scales the image automatically so that if you have it as 100px and placed in mdpi it will use it for hdpi screens but make it 150px. This is not recommended and you should always try to have all the resources into all of the folders (mdpi hdpi xhdpi).
There is a helper method that can help you convert 100dp into pixels through code.
Converting pixels to dp

Related

If our .png drawable images use a constant DP size, does it matter if we supply differnt .pngs for each screen size?

Basically title. In my layout, my images use a constant size of 200dp x 200dp. So does that mean I shouldn't bother including drawable-hdpi, drawable-xhdpi etc. as long as I use a single 200 px by 200 px image?
200px by 200px is not the same as 200dp * 200dp
Read: https://www.quora.com/What-is-the-difference-between-px-dp-dip-and-sp-in-Android
What i mean by this, is that its impossible that your image is 200dp * 200dp in every screen.
Since dp's size change in every screen, For some screens [160 dpi screens only] 200dp * 200dp is 200px * 200px, but in higher dpi screens this isn't the case, this is why if you only supply a 200px by 200px image it will look perfect in a 160dpi phone screen but in a higher dpi it will look bad (because it will look stretched.) since the higher the dpi the more pixels a dp is.
Here is a pixel to DP calculator so you can check it out for yourself.
https://pixplicity.com/dp-px-converter
That being said if you only supply one xxxhdpi image it should look perfectly fine in every screen but it takes more memory to load that image since it naturally is a higher resolution.
So if you load a xxxhdpi on a mdpi screen it will look worse than the image itself because the resolution for that phone doesn't support it but it will keep the highest quality possible for that screen.
This is why its usually best to have many images for many cases.
Example below of what is 200DP in pixels across different screens, as you can see 200DP can be from 150 pixels to 800px.
Thats why what you're suggesting of having an image in a constant DP size is impossible unless you have an image for each dpi.
Simple answer: NO.
The best way when you have only one picture for all resolutions is to get a better resolution png and you put it in drawable-xhdpi, or even more. By this way, your image will be well displayed.

Using multiple images in layout android

In my application I want to create a layout as shown below I am confused about what size of images should I keep in my resource folder.
A 1.5 inch wide mdpi display is about 240px wide but considering variable sizes for android devices I'd say to fill your screen an image 400px wide should be safe to use on mdpi devices. Scale height according to the required aspect ratio and scale both dimensions for different densities(ldpi, mdpi, hdpi, xhdpi, xxhdpi) accordingly.

If I have an image 50dp wide/tall in xhdpi, what size do I make for mdpi?

I am working with a graphic designer at the moment who is trying to verify what sizes images I am currently using in my app so he can create new images in the right size for all resolution modes (designing the image at the smallest size and then scaling it for the other modes).
In my app, I am emulating a device that is 720 x 1280: xhdpi, and on this device, an ImageView is set to size 50dp x 50 dp.
Is it correct to say 50 dp xhdpi -> 25 dp mdpi? 100px? Or 50px? What exactly? What is the correct size that we should be making for the smallest size?
According to Android Documentation.
From a base image size, there is a 3:4:6:8:12:16 scaling ratio in drawable size by DPI.
LDPI - 0.75x
MDPI - Original size // means 1.0x here
HDPI - 1.5x
XHDPI - 2.0x
XXHDPI - 3x
XXXHDPI - 4.0x
According to the upper difference you can calculate the ImageSize resoultion.
Update :
you have create different size of Image like HDPI, MDPI, XHDPI. but that is not mean that you can set ImageView Height and Width according to it's Drawable.
Example :
let's some device have small screenSize but it is high density support so you have to set high resolution density Image but the Size of the ImageView you have create small in that case.

android Supporting Multiple Screens Calculation

I recently had a doubt
I put 40px * 40 px images in drawable folder.
and put 40px * 40 px images in drawable [hdpi] again .
Then Tabhost set this picture
[hdpi] exists and does not exist
Size two pictures displayed on the screen is not the same and why?
They are all the same 40px * 40px
why?
Different folder will result in a different calculation methods it?
PS:[My phone will pre-select [hdpi] folder]
I think you need to create drawable folder in res folder and add all images in drawable folder.
Actually in android there are different resolutions screens and every resolution have fix limitation for retrieving the images on specific folders like - hdpi, ldpi ,mdpi etc. When we create drawable folder in res then if any different resolution screens will occurred then it will pic the image from drawable folder.
If running on mdpi device 150x150 px image will take up 150*150 dp of screen space.
If running on hdpi device 150x150 px image will take up 100*100 dp of screen space.
If running on xhdpi device 150x150 px image will take up 75*75 dp of screen space.
The other way around: say, you want to add an image to your application and you need it to fill 100*100 dp control, you'll need to create different size images for supported screen sizes:
100*100 px image for mdpi
150*150 px image for hdpi
200*200 px image for xhdpi

How to resize the bitmap image according to mdpi,hdpi and screen size of the mobile

I am using image say 85x85 px (putting this image in drawable-mdpi)
and if i am displaying this image of 85x85 px on [320x480]mdpi screen size device it looks good,
but while displaying this image on [480x800]mdpi device it looks very small.
I wants to know how can i resize this image of (85x85 px) so that it works fine for the device having screen width and height of 480x800, mdpi.
You have to set the image size in the xml in dp. This post is really helpful.
Consider MDPI image(let say 85x85) as baseline Create images as follow
FOLDER ImageSize Percentage
LDPI 64x64 75% of baseline
MDPI 85x85 100% BASELINE
HDPI 127x127 150% of baseline
XHDPI 170x170 200% of baseline
The better solution for this is to produce LDPI,MDPI,HDPI,XHDPI,XXHDPI sized images and place it inside the corresponding folders.
Put the image in /drawable, and define the size of the image in your XML as 85dp, it will then be scaled for LDPI/HDPI/XHDPI/XXHDPI and god forbid XXXHDPI.
However, the better solution would to produce HDPI(128px)/XHDPI(170px)/XXHDPI(255px) version of the graphics and put them in /drawable-hdpi, /drawable-xhdpi, /drawable-xxhdpi respectively. This way you can provide the best experience for your users.
[Edit]
"dp" is Android's way of defining physical size of an UI objects, so a button of 48x48dp will have roughly the same physical dimension even when run on screen sizes between 240x320 to 1080x1920, for more information check Android developer site's Supporting Multiple Screens.
[Edit 2]
For images on canvas you can use this to calculate the scaled size of the image:
DisplayMetrics dm = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(dm);
int newSize = (int)(85 * (dm.densityDpi / 160f));

Categories

Resources