Commit 0446b0bb authored by feihuaduo's avatar feihuaduo

增加屏幕工具类

targetSdkVersion升为28
parent 64155269
...@@ -13,9 +13,9 @@ buildscript { ...@@ -13,9 +13,9 @@ buildscript {
} }
ext { ext {
compileSdkVersion = 27 compileSdkVersion = 28
//buildToolsVersion = compileSdkVersion+".0.0" //buildToolsVersion = compileSdkVersion+".0.0"
supportVersion = compileSdkVersion+".1.0" supportVersion = "27.1.0"
} }
allprojects { allprojects {
......
...@@ -5,7 +5,7 @@ android { ...@@ -5,7 +5,7 @@ android {
defaultConfig { defaultConfig {
minSdkVersion 16 minSdkVersion 16
targetSdkVersion 21 targetSdkVersion 28
flavorDimensions "versionCode" flavorDimensions "versionCode"
/* ndk { /* ndk {
moduleName "YGOMobile" moduleName "YGOMobile"
......
...@@ -201,6 +201,7 @@ public class YGOMobileActivity extends NativeActivity implements ...@@ -201,6 +201,7 @@ public class YGOMobileActivity extends NativeActivity implements
} }
private void fullscreen() { private void fullscreen() {
//如果是沉浸模式
if (app().isImmerSiveMode()) { if (app().isImmerSiveMode()) {
mFullScreenUtils.fullscreen(); mFullScreenUtils.fullscreen();
app().attachGame(this); app().attachGame(this);
...@@ -223,6 +224,7 @@ public class YGOMobileActivity extends NativeActivity implements ...@@ -223,6 +224,7 @@ public class YGOMobileActivity extends NativeActivity implements
@Override @Override
public void onWindowFocusChanged(boolean hasFocus) { public void onWindowFocusChanged(boolean hasFocus) {
// Log.e("YGOMobileActivity","窗口变化"+hasFocus);
if (hasFocus) { if (hasFocus) {
fullscreen(); fullscreen();
mContentView.setHapticFeedbackEnabled(true); mContentView.setHapticFeedbackEnabled(true);
......
...@@ -7,7 +7,7 @@ android { ...@@ -7,7 +7,7 @@ android {
defaultConfig { defaultConfig {
applicationId "cn.garymb.ygomobile" applicationId "cn.garymb.ygomobile"
minSdkVersion 16 minSdkVersion 16
targetSdkVersion 26 targetSdkVersion 28
versionCode 331001201 versionCode 331001201
versionName "3.3.10" versionName "3.3.10"
flavorDimensions "versionCode" flavorDimensions "versionCode"
......
...@@ -19,6 +19,7 @@ import java.util.List; ...@@ -19,6 +19,7 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import cn.garymb.ygomobile.ui.preference.PreferenceFragmentPlus; import cn.garymb.ygomobile.ui.preference.PreferenceFragmentPlus;
import cn.garymb.ygomobile.utils.ScreenUtil;
import cn.garymb.ygomobile.utils.SystemUtils; import cn.garymb.ygomobile.utils.SystemUtils;
import static cn.garymb.ygomobile.Constants.CORE_EXPANSIONS; import static cn.garymb.ygomobile.Constants.CORE_EXPANSIONS;
...@@ -119,10 +120,12 @@ public class AppsSettings { ...@@ -119,10 +120,12 @@ public class AppsSettings {
DisplayMetrics dm = SystemUtils.getHasVirtualDisplayMetrics((Activity) context); DisplayMetrics dm = SystemUtils.getHasVirtualDisplayMetrics((Activity) context);
if (dm != null) { if (dm != null) {
int height = Math.max(dm.widthPixels, dm.heightPixels); int height = Math.max(dm.widthPixels, dm.heightPixels);
Log.i("机横屏height1", "" + height); Log.i("机横屏height1", "横屏" + height);
if(dm.widthPixels / dm.heightPixels > 16/9 ) { // if(dm.widthPixels / dm.heightPixels !=9/16 ) {
height = height - getStatusBarHeight(context); // height = height - getStatusBarHeight(context);
} // }
if (ScreenUtil.isNotchInScreen((Activity) context)&&ScreenUtil.getNotchHeight((Activity)context)!=0)
height = height - ScreenUtil.getNotchHeight((Activity)context);
if (mScreenHeight == Math.max(mScreenHeight, mScreenWidth)) { if (mScreenHeight == Math.max(mScreenHeight, mScreenWidth)) {
mScreenHeight = height; mScreenHeight = height;
} else { } else {
...@@ -132,7 +135,8 @@ public class AppsSettings { ...@@ -132,7 +135,8 @@ public class AppsSettings {
} }
Log.i("机屏幕高度", "" + mScreenHeight); Log.i("机屏幕高度", "" + mScreenHeight);
Log.i("机屏幕宽度", "" + mScreenWidth); Log.i("机屏幕宽度", "" + mScreenWidth);
Log.i("机刘海高度", "" + getNotchSize(context)); for(int i:getNotchSize(context))
Log.i("机刘海高度", "刘海高度" +i );
Log.i("机状态栏高度", "" + getStatusBarHeight(context)); Log.i("机状态栏高度", "" + getStatusBarHeight(context));
Log.i("机是否存在刘海",""+ hasNotchInScreen(context)); Log.i("机是否存在刘海",""+ hasNotchInScreen(context));
} }
......
package cn.garymb.ygomobile.utils;
import android.content.Context;
public class DensityUtils {
/**
* dp转px
*/
public static int dp2px(Context ctx, float dp) {
float density = ctx.getResources().getDisplayMetrics().density;
int px = (int) (dp * density + 0.5f);// 4.9->5 4.4->4
return px;
}
public static float px2dp(Context ctx, int px) {
float density = ctx.getResources().getDisplayMetrics().density;
float dp = px / density;
return dp;
}
}
package cn.garymb.ygomobile.utils;
import android.os.Build;
//ROM工具类
public class ROMUtil {
// 是否是小米手机
public static boolean isXiaomi() {
return "Xiaomi".equals(Build.MANUFACTURER);
}
}
package cn.garymb.ygomobile.utils;
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.Display;
import android.view.DisplayCutout;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
//屏幕工具类
public class ScreenUtil {
public static final int VIVO_NOTCH = 0x00000020;//是否有刘海
public static final int VIVO_FILLET = 0x00000008;//是否有圆角
//是否是刘海屏
public static boolean isNotchInScreen(Activity activity) {
if (!(isNotchOPPO(activity) || isNotchVivo(activity) || isNotchHuawei(activity))||isNotchXiaomi(activity)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
return isNotchP(activity);
else
return false;
}
return true;
}
//获取刘海高度
public static int getNotchHeight(Activity activity) {
int NotchHeight=0;
if ((NotchHeight=getNotchHeightOPPO(activity))!=0)
return NotchHeight;
if ((NotchHeight=getNotchHeightVivo(activity))!=0)
return NotchHeight;
if ((NotchHeight=getNotchHeightHuawei(activity))!=0)
return NotchHeight;
if ((NotchHeight=getNotchHeightXiaomi(activity))!=0)
return NotchHeight;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
if ((NotchHeight=getNotchHeightP(activity))!=0)
return NotchHeight;
}
return 0;
}
@RequiresApi(api = Build.VERSION_CODES.P)
public static int getNotchHeightP(Activity activity) {
DisplayCutout displayCutout = getDisplayCutout(activity);
if (displayCutout == null)
return 0;
return displayCutout.getSafeInsetTop();
// Log.e("TAG", "安全区域距离屏幕左边的距离 SafeInsetLeft:" + displayCutout.getSafeInsetLeft());
// Log.e("TAG", "安全区域距离屏幕右部的距离 SafeInsetRight:" + displayCutout.getSafeInsetRight());
// Log.e("TAG", "安全区域距离屏幕顶部的距离 SafeInsetTop:" + displayCutout.getSafeInsetTop());
// Log.e("TAG", "安全区域距离屏幕底部的距离 SafeInsetBottom:" + displayCutout.getSafeInsetBottom());
}
@RequiresApi(api = Build.VERSION_CODES.P)
public static DisplayCutout getDisplayCutout(Activity activity) {
return activity.getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
}
//小米的状态栏高度会略高于刘海屏的高度,因此可以通过获取状态栏的高度来间接避开刘海屏
public static int getNotchHeightXiaomi(Activity activity) {
if (isNotchXiaomi(activity))
return 0;
return getStatusBarHeight(activity);
}
//获取状态栏高度
public static int getStatusBarHeight(Context context) {
int statusBarHeight = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
}
return statusBarHeight;
}
//OPPO是否有刘海
public static boolean isNotchOPPO(Context context) {
return context.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
}
//vivo是否有刘海
public static boolean isNotchVivo(Context context) {
boolean ret = false;
try {
ClassLoader classLoader = context.getClassLoader();
Class FtFeature = classLoader.loadClass("android.util.FtFeature");
Method method = FtFeature.getMethod("isFeatureSupport", int.class);
ret = (boolean) method.invoke(FtFeature, VIVO_NOTCH);
} catch (ClassNotFoundException e) {
Log.e("Notch", "hasNotchAtVivo ClassNotFoundException");
} catch (NoSuchMethodException e) {
Log.e("Notch", "hasNotchAtVivo NoSuchMethodException");
} catch (Exception e) {
Log.e("Notch", "hasNotchAtVivo Exception");
} finally {
return ret;
}
}
//华为是否有刘海
public static boolean isNotchHuawei(Context context) {
boolean ret = false;
try {
ClassLoader classLoader = context.getClassLoader();
Class HwNotchSizeUtil = classLoader.loadClass("com.huawei.android.util.HwNotchSizeUtil");
Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
ret = (boolean) get.invoke(HwNotchSizeUtil);
} catch (ClassNotFoundException e) {
Log.e("Notch", "hasNotchAtHuawei ClassNotFoundException");
} catch (NoSuchMethodException e) {
Log.e("Notch", "hasNotchAtHuawei NoSuchMethodException");
} catch (Exception e) {
Log.e("Notch", "hasNotchAtHuawei Exception");
} finally {
return ret;
}
}
public static boolean isNotchXiaomi(Activity activity){
return getInt("ro.miui.notch",activity)==1;
}
/**
* 小米刘海屏判断.
* @return 0 if it is not notch ; return 1 means notch
* @throws IllegalArgumentException if the key exceeds 32 characters
*/
public static int getInt(String key,Activity activity) {
int result = 0;
if (ROMUtil.isXiaomi()){
try {
ClassLoader classLoader = activity.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties = classLoader.loadClass("android.os.SystemProperties");
//参数类型
@SuppressWarnings("rawtypes")
Class[] paramTypes = new Class[2];
paramTypes[0] = String.class;
paramTypes[1] = int.class;
Method getInt = SystemProperties.getMethod("getInt", paramTypes);
//参数
Object[] params = new Object[2];
params[0] = new String(key);
params[1] = new Integer(0);
result = (Integer) getInt.invoke(SystemProperties, params);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
return result;
}
//其他安卓p的机子是否有刘海
@RequiresApi(api = Build.VERSION_CODES.P)
public static boolean isNotchP(Activity activity) {
DisplayCutout displayCutout = getDisplayCutout(activity);
if (displayCutout == null)
return false;
List<Rect> rects = displayCutout.getBoundingRects();
return rects == null || rects.size() == 0;
// if (rects == null || rects.size() == 0) {
// Log.e("TAG", "不是刘海屏");
// } else {
// Log.e("TAG", "刘海屏数量:" + rects.size());
// for (Rect rect : rects) {
// Log.e("TAG", "刘海屏区域:" + rect);
// }
// }
}
//获取oppo刘海高度
public static int getNotchHeightOPPO(Context context) {
if (isNotchVivo(context)) {
//oppo刘海区域则都是宽度为324px, 高度为80px
return (int) DensityUtils.px2dp(context, 80);
}
return 0;
}
//获取vivo刘海高度
public static int getNotchHeightVivo(Context context) {
if (isNotchVivo(context)) {
//vivo不提供接口获取刘海尺寸,目前vivo的刘海宽为100dp,高为27dp。
return 27;
}
return 0;
}
//获取华为刘海高度
public static int getNotchHeightHuawei(Context context) {
if (isNotchHuawei(context))
return getNotchSizeAtHuawei(context)[1];
return 0;
}
//获取华为刘海尺寸:width、height
//int[0]值为刘海宽度 int[1]值为刘海高度
public static int[] getNotchSizeAtHuawei(Context context) {
int[] ret = new int[]{0, 0};
try {
ClassLoader cl = context.getClassLoader();
Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
Method get = HwNotchSizeUtil.getMethod("getNotchSize");
ret = (int[]) get.invoke(HwNotchSizeUtil);
} catch (ClassNotFoundException e) {
Log.e("Notch", "getNotchSizeAtHuawei ClassNotFoundException");
} catch (NoSuchMethodException e) {
Log.e("Notch", "getNotchSizeAtHuawei NoSuchMethodException");
} catch (Exception e) {
Log.e("Notch", "getNotchSizeAtHuawei Exception");
} finally {
return ret;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment