android dp(设备独立像素) sp(可缩放像素) pt(磅) in(英尺) mm(毫米) 转换为 px(像素)
来源:互联网 发布:c语言生成汇编 编辑:程序博客网 时间:2024/05/05 12:44
android单位换算,网络有很多相关转换代码。但是,我推荐使用使用官方的转换方法:TypedValue.applyDimension。
/**
* Converts an unpacked complex data value holding a dimension to its final floating
* point value. The two parameters <var>unit</var> and <var>value</var>
* are as in {@link #TYPE_DIMENSION}.
*
* @param unit The unit to convert from.
* @param value The value to apply the unit to.
* @param metrics Current display metrics to use in the conversion --
* supplies display density and scaling information.
*
* @return The complex floating point value multiplied by the appropriate
* metrics depending on its unit.
*/
public static float applyDimension(int unit, float value,
DisplayMetrics metrics)
{
switch (unit) {
case COMPLEX_UNIT_PX:
return value;
case COMPLEX_UNIT_DIP:
return value * metrics.density;
case COMPLEX_UNIT_SP:
return value * metrics.scaledDensity;
case COMPLEX_UNIT_PT:
return value * metrics.xdpi * (1.0f/72);
case COMPLEX_UNIT_IN:
return value * metrics.xdpi;
case COMPLEX_UNIT_MM:
return value * metrics.xdpi * (1.0f/25.4f);
}
return 0;
}
看到官方的转换方法后,有些人会问为什么网络的转换方法后面加了0.5?
加0.5的原因是保证返回值大于0。我们从TypedValue.complexToDimensionPixelSize方法得到印证。
/**
* Converts a complex data value holding a dimension to its final value
* as an integer pixel size. This is the same as
* {@link #complexToDimension}, except the raw floating point value is
* converted to an integer (pixel) value for use as a size. A size
* conversion involves rounding the base value, and ensuring that a
* non-zero base value is at least one pixel in size.
* The given <var>data</var> must be structured as a
* {@link #TYPE_DIMENSION}.
*
* @param data A complex data value holding a unit, magnitude, and
* mantissa.
* @param metrics Current display metrics to use in the conversion --
* supplies display density and scaling information.
*
* @return The number of pixels specified by the data and its desired
* multiplier and units.
*/
public static int complexToDimensionPixelSize(int data,
DisplayMetrics metrics)
{
final float value = complexToFloat(data);
final float f = applyDimension(
(data>>COMPLEX_UNIT_SHIFT)&COMPLEX_UNIT_MASK,
value,
metrics);
final int res = (int)(f+0.5f);
if (res != 0) return res;
if (value == 0) return 0;
if (value > 0) return 1;
return -1;
}
- android dp(设备独立像素) sp(可缩放像素) pt(磅) in(英尺) mm(毫米) 转换为 px(像素)
- Android中的像素单位详解( dp 、 sp 、 px 、 in 、 pt 、 mm )
- Android中的像素单位详解( dp 、 sp 、 px 、 in 、 pt 、 mm )
- android中像素单位dp、px、pt、sp转换
- android中像素单位dp、px、pt、sp转换
- android像素单位,px,pt,dp,dip,sp
- android中像素单位dp、px、pt、sp的比较
- Android中像素单位dp、px、pt、sp的比较
- android中像素单位dp、px、pt、sp的比较
- android中像素单位dp,px,pt,sp的比较
- android中像素单位dp、px、pt、sp的比较
- android中像素单位dp、px、pt、sp的比较
- android中像素单位dp、px、pt、sp的比较
- android中像素单位dp、px、pt、sp的比较
- android中像素单位dp、px、pt、sp的比较
- android中像素单位dp、px、pt、sp的比较
- android中像素单位dp、px、pt、sp的比较
- android中像素单位dp、px、pt、sp的比较
- Div滚动条属性及样式设置
- ASIHTTPRequest 详解
- Windows下Qt的MYSQL数据库开发步骤
- CSS控制图片、表格、背景颜色渐变
- UVA--10859--Placing Lampposts--DP
- android dp(设备独立像素) sp(可缩放像素) pt(磅) in(英尺) mm(毫米) 转换为 px(像素)
- swf放大缩小自适应显示
- 提供了 OutputStream 的输出显示控件(整理修改版)
- 第一天
- 【学习笔记三】最基本的JAVA基础[数据类型,运算符]
- MacBook上最简单的三系统安装教程
- [Javascript入门]Javascript之初体验1
- hadoop 0.20.2集群配置入门
- 各种平板电脑(IPAD2,chromebook)功耗大比拼