Anroid屏幕适配之dp2px和sp2px的正确姿势

先看完Andoid屏幕适配终极手段(小编用过最得劲的dp适配)

:https://www.jianshu.com/p/375d614401aa

private String getScreenParams() {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int heightPixels = dm.heightPixels;//高的像素
        int widthPixels = dm.widthPixels;//宽的像素
        int densityDpi = dm.densityDpi;//dpi
        float xdpi = dm.xdpi;//xdpi
        float ydpi = dm.ydpi;//ydpi
        float density = dm.density;//density=dpi/160,密度比
        float scaledDensity = dm.scaledDensity;//scaledDensity=dpi/160 字体缩放密度比
        float heightDP = heightPixels / density;//高度的dp
        float widthDP = widthPixels / density;//宽度的dp
        String str = "heightPixels: " + heightPixels + "px";
        str += "\nwidthPixels: " + widthPixels + "px";
        str += "\ndensityDpi: " + densityDpi + "dpi";
        str += "\nxdpi: " + xdpi + "dpi";
        str += "\nydpi: " + ydpi + "dpi";
        str += "\ndensity: " + density;
        str += "\nscaledDensity: " + scaledDensity;
        str += "\nheightDP: " + heightDP + "dp";
        str += "\nwidthDP: " + widthDP + "dp";

        return str;
    }

density=dpi/160,密度比
dp和dip是一样的,密度无关像素,Density Independent Pixels的缩写,以160dpi为基准。在160dpi设备 上1dp=1px,在240dpi设备上1dp=1.5px,以此类推
那么,在给控件设置宽高的时候,给textView设置字体大小的时候,dp2px,sp2px的正确姿势如下:

选定一个普遍的widthdp的手机作为适配的标本,如360widthdp的手机,按比例由dp和sp计算px

/**
     * 将dp值转换为px值,保证尺寸大小不变
     *
     * @return
     */
    public static int dpAdapt(Activity activity, float dp,float widthDpBase) {
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int heightPixels = dm.heightPixels;//高的像素
        int widthPixels = dm.widthPixels;//宽的像素
//        int densityDpi = dm.densityDpi;//dpi
//        float xdpi = dm.xdpi;//xdpi
//        float ydpi = dm.ydpi;//ydpi
        float density = dm.density;//density=dpi/160,密度比
//        float scaledDensity = dm.scaledDensity;//scaledDensity=dpi/160 字体缩放密度比
        float heightDP = heightPixels / density;//高度的dp
        float widthDP = widthPixels / density;//宽度的dp
        float w=widthDP>heightDP?heightDP:widthDP;
//        final float scale = activity.getResources().getDisplayMetrics().density;
        return (int) (dp *w/widthDpBase* density + 0.5f);
    }
    /**
     * 将sp值转换为px值,保证尺寸大小不变
     *
     * @return
     */
    public static int spAdapt(Activity activity, float sp,float widthDpBase) {
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int heightPixels = dm.heightPixels;//高的像素
        int widthPixels = dm.widthPixels;//宽的像素
//        int densityDpi = dm.densityDpi;//dpi
//        float xdpi = dm.xdpi;//xdpi
//        float ydpi = dm.ydpi;//ydpi
        float density = dm.density;//density=dpi/160,密度比
//        float scaledDensity = dm.scaledDensity;//scaledDensity=dpi/160 字体缩放密度比
        float heightDP = heightPixels / density;//高度的dp
        float widthDP = widthPixels / density;//宽度的dp
        float w=widthDP>heightDP?heightDP:widthDP;
//        final float scale = activity.getResources().getDisplayMetrics().density;
        return (int) (sp *w/widthDpBase + 0.5f);
    }

使用的正确姿势

   tv_install.setTextSize(spAdapt(18));
   tv_install.setPadding(dpAdapt(20), dpAdapt(6), dpAdapt(20), dpAdapt(6));
 /**
     * 将dip或dp值转换为px值,保证尺寸大小不变
     *
     * @return
     */
    protected int dpAdapt(float dp) {
        return ScreenUtils.dpAdapt(activity, dp, 360);
    }

    /**
     * 将dip或dp值转换为px值,保证尺寸大小不变
     *
     * @return
     */
    protected int spAdapt(float sp) {
        return ScreenUtils.spAdapt(activity, sp, 360);

    }

各位老铁有问题欢迎及时联系、指正、批评、撕逼

Github:
https://github.com/AnJiaoDe
简书:
https://www.jianshu.com/u/b8159d455c69

微信公众号
Anroid屏幕适配之dp2px和sp2px的正确姿势_第1张图片

QQ群
Anroid屏幕适配之dp2px和sp2px的正确姿势_第2张图片

你可能感兴趣的:(Android屏幕适配)