首页 » Android » Android 屏幕适配工具类

Android 屏幕适配工具类

原文 http://blog.csdn.net/cswhale/article/details/79026176

2018-01-11 02:01:19阅读(585)

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";
}

最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹