Skip to content

Instantly share code, notes, and snippets.

@hnrainll
Created May 14, 2015 03:49
Show Gist options
  • Select an option

  • Save hnrainll/9255d326d4fb9c5be9c2 to your computer and use it in GitHub Desktop.

Select an option

Save hnrainll/9255d326d4fb9c5be9c2 to your computer and use it in GitHub Desktop.
Android dp转换到px时为什么需要 +0.5f
将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.
@chenxinxun
Copy link
Copy Markdown

向上取整吧

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment