增加系统单位换算函数

This commit is contained in:
zengfantian 2015-03-29 20:04:07 +08:00
parent d8731cfde3
commit bb3bbdc954
1 changed files with 25 additions and 0 deletions

View File

@ -1,6 +1,9 @@
package com.zftlive.android.tools;
import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import com.zftlive.android.config.SysEnv;
@ -19,6 +22,28 @@ import com.zftlive.android.config.SysEnv;
*/
public class ToolUnit {
/**
* 获取当前分辨率下指定单位对应的像素大小根据设备信息
* px,dip,sp -> px
*
* Paint.setTextSize()单位为px
*
* 代码摘自TextView.setTextSize()
*
* @param unit TypedValue.COMPLEX_UNIT_*
* @param size
* @return
*/
public static float getRawSize(Context mContext, int unit, float size) {
Resources r;
if (mContext == null)
r = Resources.getSystem();
else
r = mContext.getResources();
return TypedValue.applyDimension(unit, size, r.getDisplayMetrics());
}
/**设备显示材质**/
private static DisplayMetrics mDisplayMetrics = SysEnv.getDisplayMetrics();