android中px和dp,px和sp之間的轉(zhuǎn)換方法
在Android開(kāi)發(fā)中dp和px,sp和px之間的轉(zhuǎn)換時(shí)必不可少的,網(wǎng)上流傳的方法
public class DisplayUtils {
/**
* convert px to its equivalent dp
* 將px轉(zhuǎn)換為與之相等的dp
*/
public static int px2dp(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* convert dp to its equivalent px
* 將dp轉(zhuǎn)換為與之相等的px
*/
public static int dp2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
/**
* convert px to its equivalent sp
* 將px轉(zhuǎn)換為sp
*/
public static int px2sp(Context context, float pxValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale + 0.5f);
}
/**
* convert sp to its equivalent px
* 將sp轉(zhuǎn)換為px
*/
public static int sp2px(Context context, float spValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
}
關(guān)于轉(zhuǎn)換公式中,通過(guò)類(lèi)比一元一次函數(shù),怎么看都是有問(wèn)題的,這么明顯的問(wèn)題,為什么沒(méi)人糾正,后來(lái)發(fā)現(xiàn)是自己并沒(méi)有理解,原因是float類(lèi)型在強(qiáng)轉(zhuǎn)為int類(lèi)型是,用的是去尾法,精度上有較大差異,所以通過(guò)+0.5f的方式,將去尾法轉(zhuǎn)變成四舍五入法,提高精度。
調(diào)用TypedValue類(lèi)實(shí)現(xiàn)轉(zhuǎn)換
/**
* convert dp to its equivalent px
*/
protected int dp2px(int dp){
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,getResources().getDisplayMetrics());
}
/**
* convert sp to its equivalent px
*/
protected int sp2px(int sp){
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp,getResources().getDisplayMetrics());
}
public class TypedValue {
...
/** {@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;
/** {@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;
/**
* 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;
}
...
}
對(duì)比兩種方式的差異,其實(shí)就在+0.5f上,去尾法和四舍五入法的區(qū)別,即精度問(wèn)題。
總結(jié)
以上所述是小編給大家介紹的android中px和dp,px和sp之間的轉(zhuǎn)換方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
30分鐘搞清楚Android Touch事件分發(fā)機(jī)制
30分鐘搞清楚Android Touch事件分發(fā)機(jī)制,Touch事件分發(fā)中只有兩個(gè)主角:ViewGroup和View,想要深入學(xué)習(xí)的朋友可以參考本文2016-03-03
Android手機(jī)號(hào)碼輸入框(滿(mǎn)11位自動(dòng)跳到下個(gè)輸入框)實(shí)例代碼
這篇文章主要介紹了Android手機(jī)號(hào)碼輸入框(滿(mǎn)11位自動(dòng)跳到下個(gè)輸入框)實(shí)例代碼,需要的朋友可以參考下2017-10-10
Android ContentProvider的實(shí)現(xiàn)及簡(jiǎn)單實(shí)例代碼
這篇文章主要介紹了Android ContentProvider的實(shí)現(xiàn)及簡(jiǎn)單實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android仿新浪微博發(fā)送菜單界面的實(shí)現(xiàn)
這篇文章主要介紹了Android仿新浪微博發(fā)送菜單界面的實(shí)現(xiàn),幫助大家更好的理解和學(xué)習(xí)使用Android開(kāi)發(fā),感興趣的朋友可以了解下2021-04-04
一看就喜歡的loading動(dòng)畫(huà)效果Android分析實(shí)現(xiàn)
一看就喜歡的loading動(dòng)畫(huà)效果Android分析實(shí)現(xiàn),絢爛的效果,相信大家一定會(huì)喜歡,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01
Android實(shí)現(xiàn)讀取SD卡下所有TXT文件名并用listView顯示出來(lái)的方法
這篇文章主要介紹了Android實(shí)現(xiàn)讀取SD卡下所有TXT文件名并用listView顯示出來(lái)的方法,涉及Android針對(duì)SD卡的讀取及文件遍歷等相關(guān)操作技巧,需要的朋友可以參考下2017-06-06

