欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Android开发中常用屏幕单位转换,android开发,public class

来源: javaer 分享于  点击 30984 次 点评:193

Android开发中常用屏幕单位转换,android开发,public class


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);    }}
相关栏目:

用户点评