diff --git a/appmain/utils/device/screen/DisplayUtil.java b/appmain/utils/device/screen/DisplayUtil.java new file mode 100644 index 0000000..25a80c1 --- /dev/null +++ b/appmain/utils/device/screen/DisplayUtil.java @@ -0,0 +1,188 @@ +package com.proxgrind.chameleon.utils.device.screen; + +import android.annotation.SuppressLint; +import android.app.Activity; +import android.content.Context; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.graphics.RectF; +import android.os.Build; +import android.view.View; +import android.view.animation.Animation; +import android.view.animation.CycleInterpolator; +import android.view.animation.TranslateAnimation; + +import com.proxgrind.chameleon.utils.system.LogUtils; + +import java.lang.reflect.Method; + +/** + * 屏幕显示工具类 + * + * @author DXL + */ +public class DisplayUtil { + /** + * 获取屏幕高度! + */ + public static int getWindowHeight(Context context) { + return context.getResources().getDisplayMetrics().heightPixels; + } + + /** + * 获取屏幕宽度! + */ + public static int getWindowWidth(Context context) { + return context.getResources().getDisplayMetrics().widthPixels; + } + + /** + * 测量底部导航栏的高度 + * + * @param mActivity:上下文环境 + * @return:返回测量出的底部导航栏高度 + */ + public static int getNavigationBarHeight(Activity mActivity) { + Resources resources = mActivity.getResources(); + int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); + int height = resources.getDimensionPixelSize(resourceId); + return height; + } + + /* + * navigation 是否存在! + * */ + private 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) { + LogUtils.e(e.toString()); + } + return hasNavigationBar; + } + + /** + * 将px值转换为dip或dp值,保证尺寸大小不变 + * + * @param pxValue 像素值 + * @return dip值 + */ + public static int px2dip(Context context, float pxValue) { + final float scale = context.getResources().getDisplayMetrics().density; + return (int) (pxValue / scale + 0.5f); + } + + /** + * 将dip或dp值转换为px值,保证尺寸大小不变 + * + * @param dipValue + * @return + */ + public static int dip2px(Context context, float dipValue) { + final float scale = context.getResources().getDisplayMetrics().density; + return (int) (dipValue * scale + 0.5f); + } + + /** + * 将px值转换为sp值,保证文字大小不变 + * + * @param pxValue + * @return + */ + public static int px2sp(Context context, float pxValue) { + final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; + return (int) (pxValue / fontScale + 0.5f); + } + + /** + * 将sp值转换为px值,保证文字大小不变 + * + * @param spValue + * @return + */ + public static int sp2px(Context context, float spValue) { + final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; + return (int) (spValue * fontScale + 0.5f); + } + + /** + * 显示状态栏和导航栏! + */ + public static void showStatusAndNavigationBar(Activity activity) { + View decorView = activity.getWindow().getDecorView(); + decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); + } + + /** + * 隐藏状态栏和导航栏! + */ + @SuppressLint("ObsoleteSdkInt") + public static void dismissStatusAndNavigationBar(Activity activity) { + //隐藏虚拟按键,并且全屏 + if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api + View v = activity.getWindow().getDecorView(); + v.setSystemUiVisibility(View.GONE); + } else if (Build.VERSION.SDK_INT >= 19) { + //for new api versions. + View decorView = activity.getWindow().getDecorView(); + int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION + | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN; + decorView.setSystemUiVisibility(uiOptions); + } + } + + /** + * 判断某个xy坐标是否在某个view内 + * + * @return 判断结果! + */ + public static boolean isXyInView(float x, float y, View v) { + if (v == null) return false; + int[] location = new int[2]; + // 获取控件在屏幕中的位置,返回的数组分别为控件左顶点的 x、y 的值 + v.getLocationOnScreen(location); + RectF rect = new RectF(location[0], location[1], + location[0] + v.getWidth(), location[1] + v.getHeight()); + return rect.contains(x, y); + } + + /** + * 设置晃动动画 + */ + public static void setShakeAnimation(View v) { + v.startAnimation(shakeAnimation(5)); + } + + /** + * 晃动动画 + * + * @param counts 1秒钟晃动多少下 + * @return + */ + public static Animation shakeAnimation(int counts) { + Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0); + translateAnimation.setInterpolator(new CycleInterpolator(counts)); + translateAnimation.setDuration(1000); + return translateAnimation; + } + + //检查当前系统是否已开启暗黑模式 + public static boolean isDarkModeStatus(Context context) { + int mode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; + return mode == Configuration.UI_MODE_NIGHT_YES; + } +} \ No newline at end of file