Formula px to dp and dp to px conversion
suggest changedensity is the scale from dp to px. The int cast drops the fraction. Resources.getSystem() uses the system display, which can differ from an activity configuration.
DP to pixel:
private int dpToPx(int dp)
{
return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}
Pixel to DP:
private int pxToDp(int px)
{
return (int) (px / Resources.getSystem().getDisplayMetrics().density);
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents