dp,sp,px相互转化
生活随笔
收集整理的這篇文章主要介紹了
dp,sp,px相互转化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
方法一:
public int sp2px(float sp) {return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp,getResources().getDisplayMetrics());
}public float dip2px(int dip) {return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, getResources().getDisplayMetrics());
}private int px2dp(int px) {return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, px, getResources().getDisplayMetrics());
}
方法二:
public static int dip2px(int dp) {float density = getContext().getResources().getDisplayMetrics().density;return (int) (dp * density + 0.5);
}public static int px2dip(int px) {final float scale = getContext().getResources().getDisplayMetrics().density;return (int) (px / scale + 0.5f);
}public static int px2sp(int pxValue) {final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity;return (int) (pxValue / fontScale + 0.5f);
}public static int sp2px(int spValue) {final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity;return (int) (spValue * fontScale + 0.5f);
}
總結
以上是生活随笔為你收集整理的dp,sp,px相互转化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 画布Canvas的使用
- 下一篇: 安卓adb常用简单指令