Density 屏幕简单设配(头条)

public class Density {
    private static float appDensity;
    private static float appScaledDensity;
    private static DisplayMetrics appDisplayMetrics;

    public static void setDensity(@NonNull final Application application) {
        //获取application的DisplayMetrics
        appDisplayMetrics = application.getResources().getDisplayMetrics();

        if (appDensity == 0) {
            //初始化的时候赋值
            appDensity = appDisplayMetrics.density;
            appScaledDensity = appDisplayMetrics.scaledDensity;

            //添加字体变化的监听
            application.registerComponentCallbacks(new ComponentCallbacks() {
                @Override
                public void onConfigurationChanged(Configuration newConfig) {
                    //字体改变后,将appScaledDensity重新赋值
                    if (newConfig != null && newConfig.fontScale > 0) {
                        appScaledDensity = application.getResources().getDisplayMetrics().scaledDensity;
                    }
                }

                @Override
                public void onLowMemory() {
                }
            });
        }
    }

    //此方法在BaseActivity中做初始化(如果不封装BaseActivity的话,直接用下面那个方法就好了)
    public static void setDefault(Activity activity) {
        setAppOrientation(activity, AppUtils.WIDTH);
    }

    //此方法用于在某一个Activity里面更改适配的方向
    public static void setOrientation(Activity activity, String orientation) {
        setAppOrientation(activity, orientation);
    }

    /**
     * targetDensity
     * targetScaledDensity
     * targetDensityDpi
     * 这三个参数是统一修改过后的值
     * 

* orientation:方向值,传入width或height */ private static void setAppOrientation(@Nullable Activity activity, String orientation) { float targetDensity; //获取状态栏高度 int barHeight = AppUtils.getStatusBarHeight(activity); if (orientation.equals("height")) { targetDensity = (appDisplayMetrics.heightPixels - barHeight) / 667f; } else { targetDensity = appDisplayMetrics.widthPixels / 360f; } float targetScaledDensity = targetDensity * (appScaledDensity / appDensity); int targetDensityDpi = (int) (160 * targetDensity); /** * * 最后在这里将修改过后的值赋给系统参数 * * 只修改Activity的density值 */ DisplayMetrics activityDisplayMetrics = activity.getResources().getDisplayMetrics(); activityDisplayMetrics.density = targetDensity; activityDisplayMetrics.scaledDensity = targetScaledDensity; activityDisplayMetrics.densityDpi = targetDensityDpi; } public static class AppUtils { public final static String WIDTH = "width"; public final static String HEIGHT = "height"; /** * px转dp * * @param context The context * @param px the pixel value * @return value in dp */ public static int pxToDp(Context context, float px) { DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); return (int) ((px / displayMetrics.density) + 0.5f); } /** * dp转px * * @param context * @param dp * @return */ public static int dpToPx(Context context, float dp) { DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); return (int) ((dp * displayMetrics.density) + 0.5f); } /** * 获取状态栏高度 * * @param context * @return */ public static int getStatusBarHeight(Context context) { int result = 0; int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = context.getResources().getDimensionPixelSize(resourceId); } return result; } /** * 获得屏幕高度 * * @param context * @return */ public static int getPxWidth(Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.widthPixels; } /** * 获得屏幕宽度 * * @param context * @return */ public static int getPxHeight(Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.heightPixels; } /** * 获得屏幕高度 * * @param context * @return */ public static int getDpWidth(Context context) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics dm = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(dm); int width = dm.widthPixels; // 屏幕宽度(像素) float density = dm.density; // 屏幕密度(0.75 / 1.0 / 1.5) // 屏幕宽度算法:屏幕宽度(像素)/屏幕密度 int dpWidth = (int) (width / density); // 屏幕宽度(dp) return dpWidth; } /** * 获得屏幕宽度 * * @param context * @return */ public static int getDpHeight(Context context) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics dm = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(dm); int height = dm.heightPixels; // 屏幕高度(像素) float density = dm.density; // 屏幕密度(0.75 / 1.0 / 1.5) // 屏幕宽度算法:屏幕宽度(像素)/屏幕密度 int dpHeight = (int) (height / density);// 屏幕高度(dp) return dpHeight; } } }

你可能感兴趣的:(Density 屏幕简单设配(头条))