安卓——像素

安卓——像素

像素

安卓支持的像素单位有:px(像素)、in(尺寸)、mm(毫米)、pt(磅,1/72英寸)、dp、dip、sp。其中,px、dp、sp最常用
px:是手机屏幕上可显示的最小单位,与物理设备有关;
dp:与物理设备无关,同样尺寸的屏幕,使用dp设置的尺寸一样;
sp:用于设置字体大小,会随系统字体大小自动调节字体显示的大小。
像素密度:一个dp可以显示多少个像素。
在xml文件中可以用dp、sp设置尺寸,在代码中尺寸都已px为单位。如果需要在代码中利用dp值设置尺寸,需要将dp转换为sp,转换代码如下:

/**
     * dp转px
     */
    public static int dip2px(Context context, float dpValue){
        //像素密度
        float density = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * density + 0.5f);
    }


    /**
     * px转dp
     */
    public static int px2dip(Context context, float pxValue){
        //像素密度
        float density = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / density + 0.5);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值