从托尼那里学会了Android刘海的修剪手法

Android中如何判断刘海屏?
接口调用

if(ScreenAdapterUtil.hasNotchScreen(activity))
{
	Log.d("ScreenAdapterUtil","你是刘海屏");
}
else
{
	Log.d("ScreenAdapterUtil","你是常规屏");
}

其中ScreenAdapterUtil类的代码如下

// ScreenAdapterUtil.java
package com.linxinfa.test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.DisplayCutout;
import android.view.View;
import android.view.WindowInsets;
import android.content.Intent;

public class ScreenAdapterUtil
{
	// 对外接口 /
	// 判断是否是刘海屏
	public static boolean hasNotchScreen(Activity activity)
	{
	    if (getInt("ro.miui.notch",activity) == 1 || // 小米刘海屏判断
	        hasNotchAtHuawei(activity) || // 华为刘海屏判断
	        hasNotchAtOPPO(activity) || // OPPO刘海屏判断
	        hasNotchAtVivo(activity) || // Vivo刘海屏判断
	        isAndroidP(activity) != null || // Android P版本刘海屏判断
	        getBarHeight(activity) >= 80 // 一般状态栏高度超过80可以认为就是刘海屏
	        //TODO 各种品牌
			) 
	    { 
	        return true;
	    }
	 	
	    return false;
	}
	 
	
	
	// Android P 刘海屏判断
	@TargetApi(28)
	@SuppressWarnings({"unchecked","deprecation"})
	private static DisplayCutout isAndroidP(Activity activity){
	    View decorView = activity.getWindow().getDecorView();
	    if (decorView != null && android.os.Build.VERSION.SDK_INT >= 28){
	        WindowInsets windowInsets = decorView.getRootWindowInsets();
	        if (windowInsets != null)
	            return windowInsets.getDisplayCutout();
	    }
	    return null;
	}
	
	// 获取状态栏高度
	@SuppressWarnings({"unchecked","deprecation"})
	private static int getBarHeight(Activity activity)
	{
		int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
		if(resourceId > 0)
		{
			return activity.getResources().getDimensionPixelSize(resourceId);
		}
		return 0;
	}
	
	private static boolean isXiaomi()
	{
		return "Xiaomi".equals(Build.MANUFACTURER);
	}
	
	// 小米刘海屏判断
	@SuppressWarnings({"unchecked","deprecation"})
	private static int getInt(String key, Activity activity) {
	    int result = 0;
	    if (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;
	}
	 
	// 华为刘海屏判断
	@SuppressWarnings({"finally","unchecked","deprecation"})
	private static boolean hasNotchAtHuawei(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) 
	    {
	    } 
	    catch (NoSuchMethodException e) 
	    {
	    } 
	    catch (Exception e) 
	    {
	    }
	    finally 
	    {
	        return ret;
	    }
	}
	 
	private static final int VIVO_NOTCH = 0x00000020;//是否有刘海
	private static final int VIVO_FILLET = 0x00000008;//是否有圆角
	 
	// VIVO刘海屏判断
	@SuppressWarnings({"finally","unchecked","deprecation"})
	private static boolean hasNotchAtVivo(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) 
	    {
	    } 
	    catch (NoSuchMethodException e) 
	    {
	    } 
	    catch (Exception e) 
	    {
	    } 
	    finally 
	    {
	        return ret;
	    }
	}

	// OPPO刘海屏判断
	private static boolean hasNotchAtOPPO(Context context) 
	{
	    return context.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
	}
}

参考
最全Android刘海屏适配方案–NotchTools:https://www.jianshu.com/p/f04f066a626d
Android通过SystemProperties获取手机系统主要信息:https://www.jianshu.com/p/6d3e883d0448

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林新发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值