I am giving a layout height as 64dp in layout file and observed the mismatch in calculated and actual pixel height.
HDPI device - Calculated height - 96 pixels, Actual height: 102 pixels
XXHDPI Device - Calculated height - 192 pixels, Actual height: 204 pixels
Any one can help me understand the difference in calculated and actual height.
mdpi, hdpi, xhdpi etc are "generalized densities". That is, dpi groups or ranges.
From Android guide:
For example two devices can have dpi values of 310 and 320 dpi respectively and fall in same group: xhdpi.
The calculations done by device code are using exact dpi value. While calculations based on dpi groups assume the following:
ldpi (low) ~120dpi
mdpi (medium) ~160dpi
hdpi (high) ~240dpi
xhdpi (extra-high) ~320dpi
xxhdpi (extra-extra-high) ~480dpi
xxxhdpi (extra-extra-extra-high) ~640dpi
Update:
The following code:
int widthDp = 160;
Log.i("TEST", "Actual DPI: " + getResources().getDisplayMetrics().xdpi);
float widthDevice = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,widthDp,getResources().getDisplayMetrics());
Log.i("TEST", widthDp + " dip in pixels on device: " + widthDevice);
When run on two hdpi devices:
213 DPI device:
2164-2164/com.example.android.dpitest I/TEST﹕ Actual DPI: 213.0
2164-2164/com.example.android.dpitest I/TEST﹕ 160 dip in pixels on device: 213.0
And 240 DPI device:
2852-2852/com.example.android.dpitest I/TEST﹕ Actual DPI: 240.0
2852-2852/com.example.android.dpitest I/TEST﹕ 160 dip in pixels on device: 240.0
Related
Firstly I did a lot of search to understand how it work, but I don't find simples tutorials. An exemple, this view:
These are my drawables folders:
The selected device is: Pixel 5.0 1080 x 1920 (xxhdpi).
Immagine in this resolution (1080 x 1920) I set the image view on the top in blue color (Solutis) with 700px of width and 250 px of height, how I have to resize this images for each drawables folders ?
I found this informations:
LDPI - 0.75x
MDPI - Original size // means 1.0x here
HDPI - 1.5x
XHDPI - 2.0x
XXHDPI - 3x
XXXHDPI - 4.0x
And
LDPI: Portrait: 200 X 320px
MDPI: Portrait: 320 X 480px
HDPI: Portrait: 480 X 800px
XHDPI: Portrait: 720 X 1280px
XXHDPI: Portrait: 960 X 1600px
XXXHDPI: Portrait: 1440 x 2560px
Here I don't understand why when I select my vistuel device 1080 x 1920px on the Design Edit it say xxhdpi and xxhdpi is 960 X 1600px...
And what gonna be the different sizes of the image for the differents drawables ?
If someone can publish a project exemple, I wool look on, please.
Did you check the documentation? Here is the interesting part:
Density-independent pixel (dp)
A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way.
The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.
This means if your device as a higher density a bigger image will been chosen.
I'm very confused about making drawables for Android.
Let's say I want to make a big image that will fill 30% of the screen, or a certain amount of dp, like 300dp.
What size should that image be in pixels for each screen density, and in what dpi should I save it in?
For 300dp, you need:
300px for mdpi (1x)
450px for hdpi (1.5x)
600px for xhdpi (2x)
900px for xxhdpi (3x)
1200px for xxxhdpi (4x)
https://developer.android.com/guide/practices/screens_support.html
ldpi - 0.75 * mdpi ,
mdpi - 1 * mdpi ,
hdpi - 1.5 * mdpi ,
xhdpi - 2 * mdpi ,
xxhdpi - 3 * mdpi ,
xxxhdpi - 4 * hdpi ,
so if you want to generate for mdpi screen to be 300dp the image should be resized according to the above calculation
i just wanted to know. i've read about this DPIs and i wandered all of the varieties. since i am new to this, i need some knowledge and i hope someone can help me out. so far i've seen LDPI, MDPI, HDPI, XHDPI, XXHDPI, XXXHDPI.
is there any other DPI variety other than this. and what is their respective resolutions ?. like as for qHD = 960 x 540 etc etc. i hope you understand guys, thanks.
please forgive me if my grammar is a little crappy, as english is not my native language
Android supports different screen resolutions
ldpi (low) ~120dpi
mdpi (medium) ~160dpi
hdpi (high) ~240dpi
xhdpi (extra-high) ~320dpi
xxhdpi (extra-extra-high) ~480dpi
xxxhdpi (extra-extra-extra-high) ~640dpi
1 dpi = 1 design independent pixel
ldpi device have 120 pixels in 1 inch size.
same for other densities...
we as programmer should use this conversion formulae :
pixel = dp * (density / 160)
so 240 dpi hdpi device's 1 dp will have = 1 * (240/160) = 3/2 = 1.5 pixels
and 240 dpi xxhdpi device's 1 dp will have = 1 * (480/160) = 3 pixels
Using this 1.5 and 3 pixels knowledge, programmer can design layouts for different densities
I have a background for my app in resolutions 720x1280 pixels, 1080x1920 pixels and 1440x2560 pixels.
In which folders (mdpi, hdpi, xhdpi and xxhdpi) should I put each background?
Please read the Android Documentation regarding screen sizes.
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
For example, 100x100px image on a MDPI will be the same size of a 200x200px on a XHDPI screen.
Require Screen sizes for splash :
LDPI: Portrait: 200 X 320px
MDPI: Portrait: 320 X 480px
HDPI: Portrait: 480 X 800px
XHDPI: Portrait: 720 X 1280px
XXHDPI: Portrait: 960 X 1600px
XXXHDPI: Portrait: 1440 x 2560px
Require icon Sizes for App :
http://iconhandbook.co.uk/reference/chart/android/
DP size of any device is (actual resolution / density conversion factor).
Density conversion factor for density buckets are as follows:
ldpi: 0.75
mdpi: 1.0 (base density)
hdpi: 1.5
xhdpi: 2.0
xxhdpi: 3.0
xxxhdpi: 4.0
Examples of resolution/density conversion to DP:
ldpi device of 240 X 320 px will be of 320 X 426.66 DP. 240 / 0.75 = 320 dp 320 / 0.75 = 426.66 dp
xxhdpi device of 1080 x 1920 pixels (Samsung S4, S5) will be of 360 X 640 dp. 1080 / 3 = 360 dp 1920 / 3 = 640 dp
This image show more:
For more details about DIP read here.
Check the image above I hope it will help someone.
Link to the whole article itself
Your inputs lack one important information of device dimension.
Suppose now popular phone is 6 inch(the diagonal of the display), you will have following results
DPI: Dots per inch - number of dots(pixels) per segment(line) of 1 inch.
DPI=Diagonal/Device size
Scaling Ratio= Real DPI/160.
160 is basic density (MHDPI)
DP: (Density-independent Pixel)=1/160 inch, think of it as a measurement unit
in order to know the phone resolution simply create a image with label mdpi, hdpi, xhdpi and xxhdpi. put these images in respective folder like mdpi, hdpi, xhdpi and xxhdpi. create a image view in layout and load this image.
the phone will load the respective image from a specific folder. by this you will get the phone resolution or *dpi it is using.
I found two approximations of how developer should support bitmaps for different screen sizes. One is this:
ldpi low density 120 dpi
mdpi medium density 160 dpi
hdpi high density 240 dpi
xhdpi extra high density 320 dpi
The other is this:
xhdpi: 2.0
hdpi: 1.5
mdpi: 1.0 (baseline)
ldpi: 0.75
This means that if you generate a 200x200 image for xhdpi devices, you
should generate the same resource in 150x150 for hdpi, 100x100 for
mdpi and finally a 75x75 image for ldpi devices
Does it mean that If I take as a baseline 640x360, I have to make following 4 groups of images:
1) ldpi - size 480x270 pixels and density 120 dpi
2) mdpi - size 640x360 pixels and density 160 dpi
3) hdpi - size 960x540 pixels and density 240 dpi
4) xhdpi - size 1280x720 pixels and density 320 dpi
You are misunderstanding.
The documentation is not saying different things. The second part you reference is telling you how much bigger an image is in relation to the base density of mdpi
e.g xhdpi fits 2x the amount of horizontal and vertical pixels into the same space that mdpi would.
mdpi = 160 dpi (scale factor 1)
xhdpi = 320 dpi (x2 twice as dense as mdpi therefore an image in this folder needs to be twice as high and twice as wide as its mdpi counterpart to appear the same size on an xhdpi screen)