当前位置:操作系统 > 安卓/Android >>

Android程序中像素(px)跟单位dp(dip)之间的转换

public class UnitTransformUtil { 
        /** * 根据手机的分辨率从dp 的单位 转成为px(像素) */ 
        public static int dip2px(Context context, float dpValue) { 
                final float scale = context.getResources().getDisplayMetrics().density; 
                return (int) (dpValue * scale + 0.5f); 
        } 
 
        /** * 根据手机的分辨率从px(像素) 的单位 转成为dp */ 
        public static int px2dip(Context context, float pxValue) { 
                final float scale = context.getResources().getDisplayMetrics().density; 
                return (int) (pxValue / scale + 0.5f); 
        } 
}   

摘自 z104207的专栏
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,