您的位置:首页 > 移动开发 > Android开发

Android px、dp、sp之间相互转换 系统默认12 sp

2017-02-22 10:39 387 查看
px 就是像素

sp=dpX字体比例(1.25f)

一、dp(或者dip device independent pixels)

一种基于屏幕密度的抽象单位。在每英寸160点的显示器上,1dp=1px。不同设备有不同的显示效果,这个和设备硬件有关。

android里的代码如下:

[java] view plain copy

<span style="font-size: 14px; color: rgb(51, 51, 51);">// 文件位置:android4.0\frameworks\base\core\java\android\util\DisplayMetrics.java

public static final int DENSITY_DEVICE = getDeviceDensity();

public float density;

public void setToDefaults() {

widthPixels = 0;

heightPixels = 0;

density = DENSITY_DEVICE / (float) DENSITY_DEFAULT; // 这里dp用的比例

densityDpi = DENSITY_DEVICE;

scaledDensity = density; // 这是sp用的比例

xdpi = DENSITY_DEVICE;

ydpi = DENSITY_DEVICE;

noncompatWidthPixels = 0;

noncompatHeightPixels = 0;

}

private static int getDeviceDensity() {

// qemu.sf.lcd_density can be used to override ro.sf.lcd_density

// when running in the emulator, allowing for dynamic configurations.

// The reason for this is that ro.sf.lcd_density is write-once and is

// set by the init process when it parses build.prop before anything else.

return SystemProperties.getInt("qemu.sf.lcd_density",

SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT)); // 从系统属性ro.sf.lcd_density里获取屏幕密度

// 文件位置:android4.0\packages\inputmethods\latinime\java\src\com\android\inputmethod\latin\Utils.java

public static float getDipScale(Context context) {

final float scale = context.getResources().getDisplayMetrics().density;

return scale;

}

public static int dipToPixel(float scale, int dip) {

return (int) (dip * scale + 0.5); // dip到px的换算公式

} </span>

二、sp(Scaled Pixels)

主要用于字体显示,与刻度无关的一种像素,与dp类似,但是可以根据用户的字体大小首选项进行缩放。

[java] view plain copy

<span style="color:#333333;">// 文件位置:android4.0\packages\apps\settings\src\com\android\settings\Display.java

private Spinner.OnItemSelectedListener mFontSizeChanged

= new Spinner.OnItemSelectedListener() {

public void onItemSelected(android.widget.AdapterView av, View v,

int position, long id) {

if (position == 0) { // 下面是设置字体比例的代码

mCurConfig.fontScale = .75f;

} else if (position == 2) {

mCurConfig.fontScale = 1.25f;

} else {

mCurConfig.fontScale = 1.0f;

}

updateFontScale();

}

public void onNothingSelected(android.widget.AdapterView av) {

}

};

private void updateFontScale() {

mDisplayMetrics.scaledDensity = mDisplayMetrics.density *

mCurConfig.fontScale; // 将设置的字体比例代码合到scaledDensity里去

float size = mTextSizeTyped.getDimension(mDisplayMetrics);

mPreview.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);

} </span>

转换代码如下

[java] view plaincopy

/**
* dp、sp 转换为 px 的工具类
*
* @author fxsky 2012.11.12
*
*/
public class DisplayUtil {
/**
* 将px值转换为dip或dp值,保证尺寸大小不变
*
* @param pxValue
* @param scale
*            (DisplayMetrics类中属性density)
* @return
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}

/**
* 将dip或dp值转换为px值,保证尺寸大小不变
*
* @param dipValue
* @param scale
*            (DisplayMetrics类中属性density)
* @return
*/
public static int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}

/**
* 将px值转换为sp值,保证文字大小不变
*
* @param pxValue
* @param fontScale
*            (DisplayMetrics类中属性scaledDensity)
* @return
*/
public static int px2sp(Context context, float pxValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale + 0.5f);
}

/**
* 将sp值转换为px值,保证文字大小不变
*
* @param spValue
* @param fontScale
*            (DisplayMetrics类中属性scaledDensity)
* @return
*/
public static int sp2px(Context context, float spValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
}

//第二中转换方法

private int dp2px(int value) {
float v = getContext().getResources().getDisplayMetrics().density;
return (int) (v * value + 0.5f);
}

private int sp2px(int value) {
float v = getContext().getResources().getDisplayMetrics().scaledDensity;
return (int) (v * value + 0.5f);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: