density-independent-pixel

How to determine the screen width in terms of dp or dip at runtime in Android?

女生的网名这么多〃 提交于 2019-11-26 16:54:27
I need to code the layout of the android widgets using dip/dp (in java files). At runtime if I code, int pixel=this.getWindowManager().getDefaultDisplay().getWidth() ; this return the screen width in pixels (px). To convert this to dp, I coded: int dp =pixel/(int)getResources().getDisplayMetrics().density ; This does not seem to be returning correct answer. I made the emulator of WVGA800 whose screen resolution is 480 by 800. When the run the emulator and let the code print the values of pixel and dp, it came to 320 in both. This emulator is 240 dpi whose scale factor would be 0.75. Dax Try

How to convert DP, PX, SP among each other, especially DP and SP?

て烟熏妆下的殇ゞ 提交于 2019-11-26 15:19:25
I have known the difference among DP , SP and PX . And after searching this topic, I found nothing satisfying me completely. Maybe this post is a duplicate, but I still want to know what is the formula of converting from DP to PX , and DP to SP , from SP to PX , from PX to SP , from SP to DP , from DP to SP ? I have known some codes to do this, but they are imperfect. DP to PX: public static int dpToPx(float dp, Context context) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics()); } SP to PX: public static int spToPx(float sp,

What is the correct way to specify dimensions in DIP from Java code?

て烟熏妆下的殇ゞ 提交于 2019-11-26 07:56:08
问题 I found that it is possible to set dimensions of my interface elements in XML layouts using DIPs as in following fragment: android:layout_width=\"10dip\" But all Java interface takes integer as arguments and there is no way to specify dimensions in DIPs. What is the correct way to calculate this? I figured that I have to use property density of DisplayMetrics class but is this a correct way? May I rely on this formula being always correct? pixels * DisplayMetrics.density = dip Is there a

How to determine the screen width in terms of dp or dip at runtime in Android?

落花浮王杯 提交于 2019-11-26 04:57:45
问题 I need to code the layout of the android widgets using dip/dp (in java files). At runtime if I code, int pixel=this.getWindowManager().getDefaultDisplay().getWidth() ; this return the screen width in pixels (px). To convert this to dp, I coded: int dp =pixel/(int)getResources().getDisplayMetrics().density ; This does not seem to be returning correct answer. I made the emulator of WVGA800 whose screen resolution is 480 by 800. When the run the emulator and let the code print the values of

How to convert DP, PX, SP among each other, especially DP and SP?

最后都变了- 提交于 2019-11-26 04:19:31
问题 I have known the difference among DP , SP and PX . And after searching this topic, I found nothing satisfying me completely. Maybe this post is a duplicate, but I still want to know what is the formula of converting from DP to PX , and DP to SP , from SP to PX , from PX to SP , from SP to DP , from DP to SP ? I have known some codes to do this, but they are imperfect. 回答1: DP to PX: public static int dpToPx(float dp, Context context) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX

Formula px to dp, dp to px android

十年热恋 提交于 2019-11-26 01:04:48
问题 I am trying to calculate a variable amount of pixels to density independent pixels and vice-versa. This formula (px to dp): dp = (int)(px / (displayMetrics.densityDpi / 160)); does not work on small devices because it is divided by zero. This is my dp to px formula: px = (int)(dp * (displayMetrics.densityDpi / 160)); Could someone give me some pointers? 回答1: Note: The widely used solution above is based on displayMetrics.density . However, the docs explain that this value is a rounded value,

Formula px to dp, dp to px android

喜你入骨 提交于 2019-11-25 23:52:43
I am trying to calculate a variable amount of pixels to density independent pixels and vice-versa. This formula (px to dp): dp = (int)(px / (displayMetrics.densityDpi / 160)); does not work on small devices because it is divided by zero. This is my dp to px formula: px = (int)(dp * (displayMetrics.densityDpi / 160)); Could someone give me some pointers? Bachi Note: The widely used solution above is based on displayMetrics.density . However, the docs explain that this value is a rounded value, used with the screen 'buckets'. Eg. on my Nexus 10 it returns 2, where the real value would be 298dpi