Converting dp units to pixel units
In some cases, you will need to express dimensions in dp
and then convert them to pixels. Imagine an application in which a scroll or fling gesture is recognized after the user's finger has moved by at least 16 pixels. On a baseline screen, a user's must move by 16 pixels / 160 dpi
, which equals 1/10th of an inch (or 2.5 mm) before the gesture is recognized. On a device with a high density display (240dpi), the user's must move by 16 pixels / 240 dpi
, which equals 1/15th of an inch (or 1.7 mm). The distance is much shorter and the application thus appears more sensitive to the user.
To fix this issue, the gesture threshold must be expressed in code in dp
and then converted to actual pixels. For example:
// The gesture threshold expressed in dp
private static final float GESTURE_THRESHOLD_DP = 16.0f;
// Get the screen's density scale
final float scale =getResources()
.getDisplayMetrics()
.density;
// Convert the dps to pixels, based on density scale
mGestureThreshold = (int) (GESTURE_THRESHOLD_DP * scale + 0.5f);
// Use mGestureThreshold as a distance in pixels...
The DisplayMetrics.density
field specifies the scale factor you must use to convert dp
units to pixels, according to the current screen density. On a medium-density screen, DisplayMetrics.density
equals 1.0; on a high-density screen it equals 1.5; on an extra high-density screen, it equals 2.0; and on a low-density screen, it equals 0.75. This figure is the factor by which you should multiply the dp
units on order to get the actual pixel count for the current screen. (Then add 0.5f
to round the figure up to the nearest whole number, when converting to an integer.) For more information, refer to the DisplayMetrics
class.
However, instead of defining an arbitrary threshold for this kind of event, you should use pre-scaled configuration values that are available from ViewConfiguration
.
Using pre-scaled configuration values
You can use the ViewConfiguration
class to access common distances, speeds, and times used by the Android system. For instance, the distance in pixels used by the framework as the scroll threshold can be obtained with getScaledTouchSlop()
:
private static final int GESTURE_THRESHOLD_DP = ViewConfiguration.get(myContext).getScaledTouchSlop();
Methods in ViewConfiguration
starting with the getScaled
prefix are guaranteed to return a value in pixels that will display properly regardless of the current screen density.