I found this in the Android documentation:
The conversion of dp units to screen pixels is simple:
px = dp * (dpi / 160)
But I don't understand what dpi
is and how it is calculated. Any help?
I found this in the Android documentation:
The conversion of dp units to screen pixels is simple:
px = dp * (dpi / 160)
But I don't understand what dpi
is and how it is calculated. Any help?
dpi stands for Dots Per Inch. In android it is used to denote screen density of the mobile.
Android smartphones support 4 types of dpi
You can get info on the display from the DisplayMetrics struct:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Visit http://developer.android.com/guide/practices/screens_support.html for more information about screen sizes.
dpi
stands for dots-per-inch and is a physical characteristic of any screen.
You can access the x and y dpi values by examining the xdpi
and ydpi
fields of the DisplayMetrics
class. To get the DisplayMetrics
use something like the following...
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
float x = metrics.xdpi; // Retrieve the x dpi value
float y = metrics.ydpi; // Retrieve the y dpi value
Perhaps playing with a DP calculator will illustrate how this works. This nifty converter demonstrates it best, in my opinion.
It illustrates how pixel dimensions appear as different physical dimensions between screens of different densities. For instance, 48px will appears half as big on a screen with twice the DPI.
There are various DPI bins known to Android:
The baseline DPI is 160 (mdpi), at which 1dp = 1px. At twice that value, 320 (xhdpi), 1dp = 2px.
Please go through this post, i think you will get your answer :)
What is the difference between "px", "dp", "dip" and "sp" on Android?