Created
May 14, 2015 03:49
-
-
Save hnrainll/9255d326d4fb9c5be9c2 to your computer and use it in GitHub Desktop.
Android dp转换到px时为什么需要 +0.5f
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 将dp转换为px时,我们通常的做法: | |
| public static float convertDpToPixel(float dp, Context context){ | |
| final float density = getResources().getDisplayMetrics().density; | |
| return (dp * scale + 0.5f); | |
| } | |
| 那么为什么需要+0.5f呢? | |
| 在官方文档中:http://developer.android.com/guide/practices/screens_support.html | |
| 有这样一句话: Then add 0.5f to round the figure up to the nearest whole number, when converting to an integer. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
向上取整吧