Android 屏幕适配(dp和px转换)

    xiaoxiao2021-12-12  6

    名词: px:屏幕的像素点 dp:密度无关像素(一个基于density的抽象单位,如果一个160dpi的屏幕,1dp=1pxdip ) sp:可伸缩像素 (指定文字大小)

    获取屏幕密度: float xdpi=getResources().getDisplayMetrics().xdpi;

    转换工具:

    public class DensityUtil { /** * 根据手机的分辨率从 dip 的单位 转成为 px(像素) * return px(像素) */ public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp * return dp */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } }
    转载请注明原文地址: https://ju.6miu.com/read-900269.html

    最新回复(0)