First, we have to know that different phone have different screen resolution, let's look the diagram below to gain understanding
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);
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