setWidth(int Pixels)使用dip还是px?

发布于 2024-08-24 13:30:03 字数 105 浏览 5 评论 0原文

setWidth(int Pixels) 是使用设备独立像素还是物理像素作为单位? 例如,setWidth(100) 是否将视图的宽度设置为 100 倾斜或 100 像素?

谢谢。

Does setWidth(int pixels) use device independent pixel or physical pixel as unit?
For example, does setWidth(100) set the a view's width to 100 dips or 100 pxs?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

青衫负雪 2024-08-31 13:30:04

基于上面对我来说效果很好的答案,我生成了一些辅助方法,只需将它们添加到您的实用程序中即可在整个项目中使用它们。

   // value in DP
   public static int getValueInDP(Context context, int value){
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, context.getResources().getDisplayMetrics());
    }

    public static float getValueInDP(Context context, float value){
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, context.getResources().getDisplayMetrics());
    }

    // value in PX
    public static int getValueInPixel(Context context, int value){
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, value, context.getResources().getDisplayMetrics());
    }

    public static float getValueInPixel(Context context, float value){
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, value, context.getResources().getDisplayMetrics());
    }

Based on above answers which works fine to me, i generate some helper methods, just add them in your utils to use them in whole project.

   // value in DP
   public static int getValueInDP(Context context, int value){
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, context.getResources().getDisplayMetrics());
    }

    public static float getValueInDP(Context context, float value){
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, context.getResources().getDisplayMetrics());
    }

    // value in PX
    public static int getValueInPixel(Context context, int value){
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, value, context.getResources().getDisplayMetrics());
    }

    public static float getValueInPixel(Context context, float value){
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, value, context.getResources().getDisplayMetrics());
    }
电影里的梦 2024-08-31 13:30:04

它使用像素。这是一个 Kotlin 扩展函数,用于将像素转换为 dp

fun Context.pxToDp(value: Float):Int{
    val r: Resources = resources
    return TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP, value, r.displayMetrics
    ).roundToInt()
}

It uses pixels. here's a Kotlin extension function to convert pixels to dp

fun Context.pxToDp(value: Float):Int{
    val r: Resources = resources
    return TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP, value, r.displayMetrics
    ).roundToInt()
}
十秒萌定你 2024-08-31 13:30:04

像素 当然,该方法要求像素作为参数。

Pixels of course, the method is asking for pixels as parameter.

寂寞清仓 2024-08-31 13:30:03

它使用像素,但我确信您想知道如何使用下降。答案就在 TypedValue.applyDimension()。以下是如何在代码中将 dips 转换为 px 的示例:

// Converts 14 dip into its equivalent px
Resources r = getResources();
int px = Math.round(TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP, 14,r.getDisplayMetrics()));

It uses pixels, but I'm sure you're wondering how to use dips instead. The answer is in TypedValue.applyDimension(). Here's an example of how to convert dips to px in code:

// Converts 14 dip into its equivalent px
Resources r = getResources();
int px = Math.round(TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP, 14,r.getDisplayMetrics()));
谁的年少不轻狂 2024-08-31 13:30:03

在代码中获取恒定数量的 DIP 的正确方法是创建一个包含 dp 值的资源 XML 文件,类似于:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="image_width">100dp</dimen>
    <dimen name="image_height">75dp</dimen>
</resources>

然后像这样引用代码中的资源:

float width = getResources().getDimension(R.dimen.image_width));
float height = getResources().getDimension(R.dimen.image_height));

您返回的浮点数将根据像素密度进行相应缩放设备的转换方法,因此您无需在整个应用程序中不断复制转换方法。

The correct way to obtain a constant number of DIPs in code is to create a resources XML file containing dp values a bit like:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="image_width">100dp</dimen>
    <dimen name="image_height">75dp</dimen>
</resources>

Then refer to the resource in your code like so:

float width = getResources().getDimension(R.dimen.image_width));
float height = getResources().getDimension(R.dimen.image_height));

The float you have returned will be scaled accordingly for the pixel density of the device and so you don't need to keep replicating a conversion method throughout your application.

锦欢 2024-08-31 13:30:03

方法 setWidth(100),设置 100 px 作为宽度(不是以 dp 为单位)。因此,您可能会在不同的 Android 手机上遇到宽度变化的问题。因此,请使用 dp 测量而不是像素。使用下面的代码获取样本宽度的 dp 测量=300px,高度=400px。

int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300, getResources().getDisplayMetrics());

int Height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 400, getResources().getDisplayMetrics());

Method setWidth(100), set 100 px as width(not in dp).So you may face width varying problems on different android phones.So use measurement in dp instead of pixels.Use the below code to get measurement in dp of sample width=300px and height=400px.

int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300, getResources().getDisplayMetrics());

int Height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 400, getResources().getDisplayMetrics());
痴者 2024-08-31 13:30:03
float dps = 100;
float pxs = dps * getResources().getDisplayMetrics().density;

来源(@Romain Guy)

float dps = 100;
float pxs = dps * getResources().getDisplayMetrics().density;

Source (@Romain Guy)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文