美文网首页
Android异形屏处理

Android异形屏处理

作者: 寒冷de星空 | 来源:发表于2020-06-22 19:59 被阅读0次

public class NotchActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

    //开局就一张背景图
    setContentView(R.layout.notch);

    getNotchParams();
}

@TargetApi(28)
public void getNotchParams() {
    final View decorView = getWindow().getDecorView();

    decorView.post(new Runnable() {
        @Override
        public void run() {
            WindowInsets rootWindowInsets = decorView.getRootWindowInsets();
            if (rootWindowInsets == null) {
                Log.e("TAG", "rootWindowInsets为空了");
                return;
            }
            DisplayCutout displayCutout = rootWindowInsets.getDisplayCutout();
            Log.e("TAG", "安全区域距离屏幕左边的距离 SafeInsetLeft:" + displayCutout.getSafeInsetLeft());
            Log.e("TAG", "安全区域距离屏幕右部的距离 SafeInsetRight:" + displayCutout.getSafeInsetRight());
            Log.e("TAG", "安全区域距离屏幕顶部的距离 SafeInsetTop:" + displayCutout.getSafeInsetTop());
            Log.e("TAG", "安全区域距离屏幕底部的距离 SafeInsetBottom:" + displayCutout.getSafeInsetBottom());
            
            List<Rect> rects = displayCutout.getBoundingRects();
            if (rects == null || rects.size() == 0) {
                Log.e("TAG", "不是刘海屏");
            } else {
                Log.e("TAG", "刘海屏数量:" + rects.size());
                for (Rect rect : rects) {
                    Log.e("TAG", "刘海屏区域:" + rect);
                }
            }
        }
    });
}

}

输出结果为:
06-04 21:57:10.120 5698-5698/? E/TAG: 安全区域距离屏幕左边的距离 SafeInsetLeft:0
06-04 21:57:10.120 5698-5698/? E/TAG: 安全区域距离屏幕右部的距离 SafeInsetRight:0
06-04 21:57:10.120 5698-5698/? E/TAG: 安全区域距离屏幕顶部的距离 SafeInsetTop:112
06-04 21:57:10.120 5698-5698/? E/TAG: 安全区域距离屏幕底部的距离 SafeInsetBottom:112
06-04 21:57:10.120 5698-5698/? E/TAG: 刘海屏数量:2
06-04 21:57:10.120 5698-5698/? E/TAG: 刘海屏区域:Rect(468, 0 - 972, 112)
06-04 21:57:10.120 5698-5698/? E/TAG: 刘海屏区域:Rect(468, 2448 - 972, 2560)

转载自https://www.jianshu.com/p/561f7241153b

相关文章

网友评论

      本文标题:Android异形屏处理

      本文链接:https://www.haomeiwen.com/subject/yspffktx.html