Android setWidth(int pixel) 使用的是 dip 还是 px?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2406449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does setWidth(int pixels) use dip or px?
提问by user256239
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?
setWidth(int pixel) 是使用设备独立像素还是物理像素作为单位?例如, setWidth(100) 是否将视图的宽度设置为 100 dips 或 100 pxs?
Thanks.
谢谢。
回答by Dan Lew
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:
它使用像素,但我确定您想知道如何使用倾斜。答案在TypedValue.applyDimension()
. 以下是如何在代码中将倾角转换为 px 的示例:
// Converts 14 dip into its equivalent px
Resources r = getResources();
int px = Math.round(TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 14,r.getDisplayMetrics()));
回答by SDJMcHattie
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:
在代码中获得恒定数量的 DIP 的正确方法是创建一个包含 dp 值的资源 XML 文件,有点像:
<?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.
您返回的浮点数将根据设备的像素密度进行相应缩放,因此您无需在整个应用程序中不断复制转换方法。
回答by Nidhin
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.
方法 setWidth(100),将 100 px 设置为宽度(不在 dp 中)。因此您可能会在不同的 android 手机上遇到宽度变化的问题。所以使用 dp 中的测量而不是像素。使用下面的代码来获得示例宽度的 dp 中的测量= 300 像素和高度 = 400 像素。
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300, getResources().getDisplayMetrics());
int Height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 400, getResources().getDisplayMetrics());
回答by pomber
float dps = 100;
float pxs = dps * getResources().getDisplayMetrics().density;
回答by Abhishek Garg
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());
}
回答by Josnidhin
Pixels of course, the method is asking for pixels as parameter.
像素当然,该方法要求像素作为参数。