Android 螢幕適配工具類

cswhale發表於2018-01-10
package com.demo.factory;

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

import com.demo.ui.activity.basic.Base;
import com.demo.config.Config;
import com.demo.utils.LogUtils;

import java.lang.reflect.Method;

public class Axis {
    private static int w;
    private static int h;
    private static float scaledDensity;


    public static void init(Context context) {
        DisplayMetrics metrics = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        w = metrics.widthPixels;
        LogUtils.e("w------------",w+"");
        h = metrics.heightPixels;
        scaledDensity = metrics.scaledDensity;
    }

    public static int scaleX(int x) {
        return scaleX(x, 0);
    }

    public static int scaleY(int y) {
        return scaleY(y, 0);
    }

    public static int toDesignX(int x) {
        return (int) (x * Config.width / w);
    }

    public static int toDesignY(int y) {
        return (int) (y * Config.height / h);
    }

    public static int scaleX(int x, float ox) {
        return (int) (x * (w - w * ox) / (Config.width - Config.width * ox));
    }

    public static int scaleY(int y, float oy) {
        return (int) (y * (h - h * oy) / (Config.height - Config.height * oy));
    }

    public static int getWidth() {
        return w;
    }

    public static int getHeight() {
        return h;
    }

    public static int scale(int x) {
        return x * Math.min(w, h) / Config.width;
    }

    public static float scaleTextSize(int textSize) {
        return scale(textSize) / scaledDensity;
    }


    /**
     * 是否開啟了虛擬按鍵
     * 
     * @param context
     * @return
     */
    private static boolean checkDeviceHasNavigationBar(Context context) {
        boolean hasNavigationBar = false;
        Resources rs = context.getResources();
        int id = rs
                .getIdentifier("config_showNavigationBar", "bool", "android");
        if (id > 0) {
            hasNavigationBar = rs.getBoolean(id);
        }
        try {
            Class systemPropertiesClass = Class
                    .forName("android.os.SystemProperties");
            Method m = systemPropertiesClass.getMethod("get", String.class);
            String navBarOverride = (String) m.invoke(systemPropertiesClass,
                    "qemu.hw.mainkeys");
            if ("1".equals(navBarOverride)) {
                hasNavigationBar = false;
            } else if ("0".equals(navBarOverride)) {
                hasNavigationBar = true;
            }
        } catch (Exception e) {

        }

        return hasNavigationBar;

    }

    /**
     * 獲取虛擬按鍵的高度
     * 
     * @param context
     * @return
     */
    public static int getNavigationBarHeight(Context context) {
        int navigationBarHeight = 0;
        Resources rs = context.getResources();
        int id = rs.getIdentifier("navigation_bar_height", "dimen", "android");
        if (id > 0 && checkDeviceHasNavigationBar(context)) {
            navigationBarHeight = rs.getDimensionPixelSize(id);
        }
        return navigationBarHeight;
    }

}

Config 類

package com.demo.config;


public class Config {
    public static int width = 1080;
    public static int height = 1920;

    //public static boolean isControlButtonRun = false;
    public static float RatioOfHeaderHeightToRefresh = 0.8f;

    public static String ACACHE_NAVISEARCH_STRING = "ACACHE_NAVISEARCH_STRING";
}

相關文章