Android使⽤TypedValue将dpsp等转换为px的⽅式介绍⼀、之前习惯的dp转px和sp转px的写法
t.Context;
/**
* 尺⼨转换⼯具
* <p>
* Created by ouyangpeng on 2015/9/21.
*/
public class SizeConvertUtil {
/**
* 根据⼿机的分辨率从 dp 的单位转成为 px(像素)
*
* @param context 上下⽂
* @param dp dp的值
* @return px的值
*/
public static int dpTopx(Context context, float dp) {
final float scale = Resources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}
/**瘦身健美操视频
* 根据⼿机的分辨率从 px(像素) 的单位转成为 dp
*
第五代人民币
* @param context 上下⽂
* @param px px的值
* @return dp的值
*/
public static int pxTodp(Context context, float px) {
final float scale = Resources().getDisplayMetrics().density;
return (int) (px / scale + 0.5f);
}
/**
* 将px值转换为sp值,保证⽂字⼤⼩不变
*
* @param context 上下⽂
* @param px px的值
* @return sp的值
*/
public static int pxTosp(Context context, float px) {
//DisplayMetrics类中属性scaledDensity
final float fontScale = Resources().getDisplayMetrics().scaledDensity;
return (int) (px / fontScale + 0.5f);
}
/**
河北科技大学分数线* 将sp值转换为px值,保证⽂字⼤⼩不变
诗朗诵配乐纯音乐*
* @param context 上下⽂
* @param sp sp的值
* @return px的值
*/
public static int spTopx(Context context, float sp) {
//DisplayMetrics类中属性scaledDensity
final float fontScale = Resources().getDisplayMetrics().scaledDensity;
return (int) (sp * fontScale + 0.5f);
}
}
⼆、另⼀种转换⽅式:TypedValue
2.1 android.widget.TextView的源码
今天在看android.widget.TextView的源码的时候,发现使⽤了 TypedValue。
android.widget.TextView#tTextSize(float) ⽅法源代码如下
/**
* Set the default text size to the given value, interpreted as "scaled
* pixel" units. This size is adjusted bad on the current density and
* ur font size preference.
*
* <p>Note: if this TextView has the auto-size feature enabled than this function is no-op. *
* @param size The scaled pixel size.
*
* @attr ref android.R.styleable#TextView_textSize
*/
@android.view.RemotableViewMethod
public void tTextSize(float size) {
tTextSize(TypedValuePLEX_UNIT_SP, size);
}
使⽤了TypedValuePLEX_UNIT_SP 这个单位,
拧成一股/** {@link #TYPE_DIMENSION} complex unit: Value is raw pixels. */
public static final int COMPLEX_UNIT_PX = 0;
/** {@link #TYPE_DIMENSION} complex unit: Value is Device Independent
* Pixels. */
public static final int COMPLEX_UNIT_DIP = 1;
/** {@link #TYPE_DIMENSION} complex unit: Value is a scaled pixel. */
public static final int COMPLEX_UNIT_SP = 2;it规划
/** {@link #TYPE_DIMENSION} complex unit: Value is in points. */
public static final int COMPLEX_UNIT_PT = 3;
/** {@link #TYPE_DIMENSION} complex unit: Value is in inches. */
public static final int COMPLEX_UNIT_IN = 4;
/
** {@link #TYPE_DIMENSION} complex unit: Value is in millimeters. */
public static final int COMPLEX_UNIT_MM = 5;
上⾯调⽤的⽅法 android.widget.TextView#tTextSize(int, float)源代码如下
/**
* Set the default text size to a given unit and value. See {@link
* TypedValue} for the possible dimension units.
*
* <p>Note: if this TextView has the auto-size feature enabled than this function is no-op. *
* @param unit The desired dimension unit.
* @param size The desired size in the given units.
*
* @attr ref android.R.styleable#TextView_textSize
*/
public void tTextSize(int unit, float size) {
if (!isAutoSizeEnabled()) {
tTextSizeInternal(unit, size, true /* shouldRequestLayout */);
}
}
上⾯调⽤的⽅法 android.widget.TextView#tTextSizeInternal 源代码如下
private void tTextSizeInternal(int unit, float size, boolean shouldRequestLayout) { Context c = getContext();
Resources r;
if (c == null) {
r = System();
} el {
r = c.getResources();
}
tRawTextSize(TypedValue.applyDimension(unit, size, r.getDisplayMetrics()),
shouldRequestLayout);
}
2.2 android.util.TypedValue#applyDimension⽅法的源码
上⾯调⽤的⽅法 android.util.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 u 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) {
ca COMPLEX_UNIT_PX:
return value;
ca COMPLEX_UNIT_DIP:
return value * metrics.density;
ca COMPLEX_UNIT_SP:
return value * metrics.scaledDensity;
ca COMPLEX_UNIT_PT:
return value * metrics.xdpi * (1.0f/72);
ca COMPLEX_UNIT_IN:
return value * metrics.xdpi;
ca COMPLEX_UNIT_MM:
return value * metrics.xdpi * (1.0f/25.4f);
}
return 0;
}
2.3 android.util.TypedValue#applyDimension⽅法的使⽤
这个⽅法的作⽤是 把Android系统中的⾮标准度量尺⼨转变为标准度量尺⼨ (Android系统中的标准尺⼨是px, 即像素)
标准单位
px (px是安卓系统内部使⽤的单位, dp是与设备⽆关的尺⼨单位 )
⾮标准单位
dp, in, mm, pt, sp
TypedValue.applyDimension()⽅法的功能就是把⾮标准尺⼨转换成标准尺⼨, ⽐如: