Android開發中常用屏幕單位轉換
public class DisplayTool { //要根據實際情況獲取到上下文Context,也可以在方法中加入context作為參數 private static final float density = context.getResources().getDisplayMetrics().density; private static final float scaledDensity =context.getResources().getDisplayMetrics().scaledDensity;/** * 將px值轉換為dip或dp值,保證尺寸大小不變 */ public static int px2dip(float pxValue) { return (int) (pxValue / density + 0.5f); } /** * 將dip或dp值轉換為px值,保證尺寸大小不變 */ public static int dip2px(float dipValue) { return (int) (dipValue * density + 0.5f); } /** * 將px值轉換為sp值,保證文字大小不變 */ public static int px2sp(float pxValue) { return (int) (pxValue / scaledDensity + 0.5f); } /** * 將sp值轉換為px值,保證文字大小不變 */ public static int sp2px(float spValue) { return (int) (spValue * scaledDensity + 0.5f); }
}</pre>
本文由用戶 f663x 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!