package com.matao.common.util;

import android.content.Context;
import android.util.DisplayMetrics;

/**
 * Created by matao on 2016-12-11 16:19
 * <p>
 * ScreenUtils提供了获取屏幕宽高以及dp与px相互转换的方法
 */

public class ScreenUtils {
    private ScreenUtils() {
        throw new AssertionError("ScreenUtils cannot be instantiated!");
    }

    public static int getScreenWidth(Context context) {
        if (context == null) {
            throw new NullPointerException("context cannot be null");
        }
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return dm.widthPixels;
    }

    public static int getScreenHeight(Context context) {
        if (context == null) {
            throw new NullPointerException("context cannot be null");
        }
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return dm.heightPixels;
    }
}

@@ -877,11 +877,783 @@
 s;%0A    %7D
+%0A%0A    public static int px2dp(Context context, float px) %7B%0A        float density = context.getResources().getDisplayMetrics().density;%0A        return (int) (px / density + 0.5f);%0A    %7D%0A%0A    public static int dp2px(Context context, float dp) %7B%0A        float density = context.getResources().getDisplayMetrics().density;%0A        return (int) (dp * density + 0.5f);%0A    %7D%0A%0A    public static int px2sp(Context context, float px) %7B%0A        float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;%0A        return (int) (px / scaledDensity + 0.5f);%0A    %7D%0A%0A    public static int sp2px(Context context, float sp) %7B%0A        float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;%0A        return (int) (sp * scaledDensity + 0.5f);%0A    %7D
 %0A%7D%0A

Add transformation between dp sp and px