识别不同屏幕中的dp大小

时间:2021-12-13 20:04:51

if a Custom View of size 500w*600h in dp , remain same as px in (1152w*720h)px screens of android , then what will be view's size in dp and px on screen of (480w*600h)px screens. And how to calculate for different size of View.

如果dp的大小为500w * 600h的自定义视图与android的(1152w * 720h)px屏幕中的px保持相同,那么在(480w * 600h)px屏幕的屏幕上dp和px中的视图大小是多少。以及如何计算不同大小的View。

2 个解决方案

#1


0  

The dp / px ratio is based on the density of the screen of the device.

dp / px比率基于设备屏幕的密度。

I would encourage you to read the Android docs on the subject.

我鼓励您阅读有关该主题的Android文档。

http://developer.android.com/guide/practices/screens_support.html

Each classification of screen density has a specific px multiplier associated with it i.e. mdpi = px * 1 and hdpi = px * 1.5

屏幕密度的每个分类都有一个特定的px乘数,即mdpi = px * 1和hdpi = px * 1.5

Here is a nice little calculator to help you make sense of it:

这是一个很好的小计算器,可以帮助你理解它:

http://labs.rampinteractive.co.uk/android_dp_px_calculator/

#2


0  

Assuming that you specified your view sizes in dp(same as dip), you can use get an instance of DisplayMetrics to convert the dp to actual pixels for your current device.

假设您在dp中指定了视图大小(与dip相同),您可以使用获取DisplayMetrics的实例将dp转换为当前设备的实际像素。

A handy function you can add to a utility class for doing conversions:

一个方便的函数,您可以添加到实用程序类进行转换:

private static float dipToPixels(Context context, int dip)
{
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, metrics);
}

#1


0  

The dp / px ratio is based on the density of the screen of the device.

dp / px比率基于设备屏幕的密度。

I would encourage you to read the Android docs on the subject.

我鼓励您阅读有关该主题的Android文档。

http://developer.android.com/guide/practices/screens_support.html

Each classification of screen density has a specific px multiplier associated with it i.e. mdpi = px * 1 and hdpi = px * 1.5

屏幕密度的每个分类都有一个特定的px乘数,即mdpi = px * 1和hdpi = px * 1.5

Here is a nice little calculator to help you make sense of it:

这是一个很好的小计算器,可以帮助你理解它:

http://labs.rampinteractive.co.uk/android_dp_px_calculator/

#2


0  

Assuming that you specified your view sizes in dp(same as dip), you can use get an instance of DisplayMetrics to convert the dp to actual pixels for your current device.

假设您在dp中指定了视图大小(与dip相同),您可以使用获取DisplayMetrics的实例将dp转换为当前设备的实际像素。

A handy function you can add to a utility class for doing conversions:

一个方便的函数,您可以添加到实用程序类进行转换:

private static float dipToPixels(Context context, int dip)
{
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, metrics);
}