Android 螢幕適配工具類
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";
}
相關文章
- android 螢幕適配Android
- Android 主流螢幕以及適配Android
- android螢幕適配詳解Android
- Android 螢幕適配終結者Android
- Android開發之螢幕適配Android
- android螢幕適配的問題Android
- Android 螢幕適配最佳實踐Android
- Flutter螢幕適配Flutter
- UIWebView 適配螢幕UIWebView
- android螢幕適配三:通過畫素密度適配Android
- Android螢幕適配(理論適配100%機型)Android
- Android螢幕適配總結和思考Android
- Android技能樹 — 螢幕適配小結Android
- 小豬淺談Android螢幕適配Android
- flutter 螢幕尺寸適配 字型大小適配Flutter
- Android螢幕適配前先了解這些Android
- android 今日頭條的螢幕適配理解Android
- 安卓螢幕適配的方案安卓
- Android dp方式的螢幕適配工具使用(Android Studio外掛方式)Android
- Android 螢幕適配:最全面的解決方案Android
- android 螢幕適配二:手寫百分比佈局適配Android
- android 螢幕適配一:通過自定義View的方式實現適配AndroidView
- Flutter螢幕適配 - 等比縮放Flutter
- 【postcss-px-to-viewport】螢幕適配CSSView
- Android最全螢幕適配的幾個重要概念(三)Android
- 移動 web 端螢幕適配 – remWebREM
- H5 分層螢幕適配H5
- 移動APP測試-Android螢幕適配問題(一)APPAndroid
- 移動APP測試:Android螢幕適配問題二APPAndroid
- Android螢幕適配dp、px兩套解決辦法Android
- 【移動適配】移動Web怎麼做螢幕適配(一)Web
- 【移動適配】移動Web怎麼做螢幕適配(三)Web
- android螢幕適配方法Android
- Android螢幕適配方案Android
- Android 螢幕適配方案Android
- Android 螢幕自適應Android
- 極其簡單的Flutter 螢幕適配Flutter
- Android螢幕適配很難嘛?其實也就那麼回事Android