Android开发中常用屏幕单位转换

2015-03-14 0 293
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);
    }

}

遇见资源网 java Android开发中常用屏幕单位转换 http://www.ox520.com/8959.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务