Wednesday 1 March 2017

Android conversion between PX and DP


First, we have to know that different phone have different screen resolution, let's look the diagram below to gain understanding



Next, the problem is when you are drawing using Canvas in Android, you will get problem of resizing of your drawing in different screen size of devices.

The equations of conversion between PX and DP are:

Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();

float dp = px / ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);

float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);


No comments:

Post a Comment