Skip to content Skip to sidebar Skip to footer

Converting Pixels To Dpi For Mdpi And Hdpi Screens

I am using this code that I have found on another thread which is working fine on mdpi screens: public static float convertDpToPixel(float dp,Context context){ Resources re

Solution 1:

Try this method:

publicfloatdpToPx(float dp, Context context){
    float density = context.getResources().getDisplayMetrics().density;
    return dp * density;
}

Solution 2:

From the doc:

metrics.density 

is the scaling factor you are looking for.

This is a scaling factor for the Density Independent Pixel unit, where one DIP is one pixel on an approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), providing the baseline of the system's display. Thus on a 160dpi screen this density value will be 1; on a 120 dpi screen it would be .75; etc.

Post a Comment for "Converting Pixels To Dpi For Mdpi And Hdpi Screens"