android.util.TypedValue.applyDimension

先看一个例子:

int size = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3,
  context.getResources().getDisplayMetrics());

这行代码是把在当前屏幕分辨率的状态下将dip为3的值转换成单位为px值  

下面是API介绍:

public static float applyDimension (int unit, float value, DisplayMetrics metrics)

Since: API Level 1

Converts an unpacked complex data value holding a dimension to its final floating point value. The two parameters unit and value are as in TYPE_DIMENSION.

Parameters
unit The unit to convert from.
value The value to apply the unit to.
metrics Current display metrics to use in the conversion -- supplies display density and scaling information.
Returns
  • The complex floating point value multiplied by the appropriate metrics depending on its unit.

查看其在framework中的源码

    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;
    }

如果传入的unit为COMPLIEX_UNIT_PX,则返回值与传入的value值保持一致;反之会根据一定的计算方式转换成px并返回。

时间: 2024-08-03 15:56:08

android.util.TypedValue.applyDimension的相关文章

android中TypedValue.applyDimension()的作用

android TypedValue.applyDimension()的作用这个方法是转变为标准尺寸的一个函数,例如int size =(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20,context.getResources().getDisplayMetrics()); 这里COMPLEX_UNIT_SP是单位,20是数值,也就是20sp.

android TypedValue.applyDimension()的探究

这个方法是转变为标准尺寸的一个函数,例如 int size = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, context.getResources().getDisplayMetrics()); 这里COMPLEX_UNIT_DIP是单位,20是数值,也就是20dp. 详细探究: //转换dip为px 2 public static int convertDipOrPx(Context context, int

TypedValue.applyDimension 中dp和sp之间转化的真相

最近在看了许多关于dp-px,px-dp,sp-px,px-sp之间转化的博文,过去我比较常用的方式是: 1 //转换dip为px 2 public static int convertDipOrPx(Context context, int dip) { 3 float scale = context.getResources().getDisplayMetrics().density; 4 return (int)(dip*scale + 0.5f*(dip>=0?1:-1)); 5 } 6

[Android Pro] Android TypedValue.applyDimension()的用法

reference to  : http://blog.csdn.net/voo00oov/article/details/45745819 这个方法的作用是 把Android系统中的非标准度量尺寸转变为标准度量尺寸 (Android系统中的标准尺寸是px, 即像素) Android系统中的尺寸单位有: 这个方法的作用是 把Android系统中的非标准度量尺寸转变为标准度量尺寸 (Android系统中的标准尺寸是px, 即像素) Android系统中的尺寸单位有: 标准单位: px (px是安卓

android TypedValue.applyDimension()的作用

这个方法是转变为标准尺寸的一个函数,例如 int size = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, context.getResources().getDisplayMetrics()); 这里COMPLEX_UNIT_SP是单位,20是数值,也就是20sp.

android.util.AndroidRuntimeException: requestFeature() must be called before adding content解决办法

最近在学习第一行代码这本书,里面的关于activity生命周期有一段例子,但是我自己用mac上装的as运行一直出问题,看log的话就是android.util.AndroidRuntimeException: requestFeature() must be called before adding content这个错误,从网上找了半天,终于找到了解决办法,虽然没有太明白原理,但是在这里做个分享吧. 这个是修改之后的代码 1 protected void onCreate(Bundle sav

[转]Java Code Examples for android.util.JsonReader

[转]Java Code Examples for android.util.JsonReader The following are top voted examples for showing how to use android.util.JsonReader. These examples are extracted from open source projects. You can vote up the examples you like and your votes will b

Android.util.Log 关于Android开发中打印log

日常Android开发真机调试过程经常会遇到系统日志过多过快,想看的内容一闪而过的问题.而自定义些log可以很好的解决这些问题.   代码中添加 log  androidsdk中提供了log输出的api,方法在android.util.Log类中. Log.v(tag,message);        //verbose模式,打印最详细的日志 Log.d(tag,message);        //debug的日志 Log.i(tag,message);        //info的日志 Lo

Android真机调试 Android.Util.AndroidRuntimeException: You cannot combine custom titles with other title features

参考连接:http://blog.csdn.net/scyatcs/article/details/9003285 Android.Util.AndroidRuntimeException: You cannot combine custom titles with other title features 错误如下图: 原文图: 解决方法: 1.在AndroidManifest.xml 中更改<application>标签中的android:theme="@style/AppThe