Tuesday, April 10, 2012

converting pixels to dp in android


I have created my application with the height and width given in pixel for a pantech device whose resolution is 480x800.



I need to convert the the height and width for an G1 device. I thought converting it into dp will solve the prob and provide same solution for both the device.



is there any easy way to convert the pixels to dp?? or any suggestions??


Source: Tips4all

8 comments:

  1. /// Converts 14 dip into its equivalent px

    Resources r = getResources();
    float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, r.getDisplayMetrics());

    ReplyDelete
  2. According to the Android Development Guide:

    px = dp * (dpi / 160)

    But often you'll want do perform this the other way around when you receive a design that's stated in pixels. So:

    dp = px / (dpi / 160)

    If you're on a 240dpi device this ratio is 1.5 (like stated before), so this means that a 60px icon equals 40dp in the application.

    ReplyDelete
  3. /**
    * This method convets dp unit to equivalent device specific value in pixels.
    *
    * @param dp A value in dp(Device independent pixels) unit. Which we need to convert into pixels
    * @param context Context to get resources and device specific display metrics
    * @return A float value to represent Pixels equivalent to dp according to device
    */
    public static float convertDpToPixel(float dp,Context context){
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * (metrics.densityDpi/160f);
    return px;
    }
    /**
    * This method converts device specific pixels to device independent pixels.
    *
    * @param px A value in px (pixels) unit. Which we need to convert into db
    * @param context Context to get resources and device specific display metrics
    * @return A float value to represent db equivalent to px value
    */
    public static float convertPixelsToDp(float px,Context context){
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float dp = px / (metrics.densityDpi / 160f);
    return dp;

    }

    ReplyDelete
  4. You should use dp just as you would pixels. That's all they are; display independent pixels. Use the same numbers you would on a medium density screen, and the size will be magically correct on a high density screen.

    However, it sounds like what you need is the fill_parent option in your layout design. Use fill_parent when you want your view or control to expand to all the remaining size in the parent container.

    ReplyDelete
  5. PX and DP are different but similar.

    DP is the resolution when you only factor the physical size of the screen. When you use DP it will scale your layout to other similar sized screens with different pixel densities.

    Occasionally you actually want pixels though, and when you deal with dimensions in code you are always dealing with real pixels, unless you convert them.

    So on a android device, normal sized hdpi screen, 800x480 is 533x320 in DP (I believe). To convert DP into pixels /1.5, to convert back *1.5. This is only for the one screen size and dpi, it would change depending on design. Our artists give me pixels though and I convert to DP with the above 1.5 equation.

    ReplyDelete
  6. use something like this

    float sizeInDip = 10f;
    int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sizeInDip, getResources().getDisplayMetrics());


    Naturally you’ll need access to your resources (context.getResources() etc.). Note that I’ve just cast this to an int here, you may want to round it up either.

    ReplyDelete
  7. float density = context.getResources().getDisplayMetrics().density;
    float px = someDpValue * density;
    float dp = somePxValue / density;


    'density' equals 1.0 on mdpi, 1.5 on hdpi and 0.75 on ldpi.

    ReplyDelete
  8. You can therefore use the following formulator to calculate the right amount of pixels for a dimension specified in dp

    public int convertToDp(int input) {
    // Get the screen's density scale
    final float scale = getResources().getDisplayMetrics().density;
    // Convert the dps to pixels, based on density scale
    return (int) (input * scale + 0.5f);
    }

    ReplyDelete