【发布时间】:2015-07-21 23:05:43
【问题描述】:
如果Custom View 在dp 中的大小为500w*600h,与(1152w*720h)px android 屏幕中的px 相同,那么dp 和px 在@ 屏幕上的视图大小将是多少987654328@ 屏幕。以及如何计算不同大小的View。
【问题讨论】:
标签: android screen android-custom-view screen-resolution
如果Custom View 在dp 中的大小为500w*600h,与(1152w*720h)px android 屏幕中的px 相同,那么dp 和px 在@ 屏幕上的视图大小将是多少987654328@ 屏幕。以及如何计算不同大小的View。
【问题讨论】:
标签: android screen android-custom-view screen-resolution
dp / px 比率基于设备屏幕的密度。
我建议您阅读有关该主题的 Android 文档。
http://developer.android.com/guide/practices/screens_support.html
每个屏幕密度分类都有一个与之关联的特定 px 乘数,即 mdpi = px * 1 和 hdpi = px * 1.5
这是一个不错的小计算器,可以帮助您理解它:
【讨论】:
假设您在 dp 中指定了视图大小(与 dip 相同),您可以使用 get 一个 DisplayMetrics 实例将 dp 转换为当前设备的实际像素。
一个方便的函数,您可以添加到实用程序类中进行转换:
private static float dipToPixels(Context context, int dip)
{
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, metrics);
}
【讨论】:
Custom View size,自定义视图不存在上下文,所以 DisplayMetrics 没用!