Skip to main content

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

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

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex