android刘海屏适配(刘海屏或圆角适配)

安卓P版本也就是刚发布的安卓9.0其中就加入了刘海屏相关的API。其相关行为与功能变更也是非常多的,从其变更的趋势及功能来看,google在进一步的收紧权限。其P版本变更行为中就加入了对非SDK接口使用的新限制。无论是直接,通过反射还是通过JNI。只要应用程序引用非SDK接口或尝试使用反射或JNI获取其句柄,就会应用这些限制。通常,应用程序应仅使用SDK中类的官方记录部分。特别是,这意味着…

大家好,又见面了,我是你们的朋友全栈君。

安卓P版本也就是刚发布的安卓9.0

其中就加入了刘海屏相关的API。其相关行为与功能变更也是非常多的,从其变更的趋势及功能来看,google在进一步的收紧权限。

其P版本变更行为中就加入了对非SDK接口使用的新限制。无论是直接,通过反射还是通过JNI。只要应用程序引用非SDK接口或尝试使用反射或JNI获取其句柄,就会应用这些限制。通常,应用程序应仅使用SDK中类的官方记录部分。特别是,这意味着当您通过诸如反射之类的语义与类交互时,您不应该计划访问SDK中未列出的方法或字段。使用此类方法或字段存在破坏应用程序的高风险。

对于非SDK行为的限制,其中google列出了灰名单/黑名单,其对应的方法和属性非常之多,目前还没来得及查找。当然谷歌也提供了检查我们应用中是否含有这些非限制的行为,具体办法见https://developer.android.google.cn/about/versions/pie/restrictions-non-sdk-interfaces。当然这个不是我们今天要讲的重点,对于非SDK行为的限制我会再抽时间好好研究后再发布一篇博客。

在安卓P发布公测版到7月底,想必不少开发者都收到了来自各大应用市场发来的邮件。

邮件内容以其中一家为例(具体市场名称已隐匿):

尊敬的开发者:
您好!为保障用户的使用体验,应用市场已在3月启动Android P版本应用适配检测工作,
针对未做适配的应用开发者陆续进行邮件通知。
请于2018年7月底前务必完成Android  P版本适配工作并自检通过。
针对未适配或在Android P版本体验欠佳的应用,
应用市场将在Android  P版本机型上采取屏蔽或不推荐更新策略,
可能会对您的推广、用户口碑及品牌产生影响。
相关技术资料见:https://developer.android.google.cn/preview/overview.html
请各相关开发者及时调整。

这样就导致我们不得不去适配安卓P了。

在收到邮件后,查阅谷歌官方的资料,更新SDK。其中UI最大的改变就是需要适配刘海屏。

但是国内厂商为了快速抢占全面屏手机市场,在安卓系统8.1的基础上,硬件就弄出了刘海屏,例如华为nova 3e, oneplus 6, oppo r15, vivo x21,但是系统支持没有更上,没有一个统一的API。但发现他们的分辨率都出奇的接近,就像达成了默契,2280 * 1080,高宽比达到了2.1。

今天就来说说怎么适配安卓8.1及P上的刘海屏。

首先下载谷歌最新的SDK API 28及系统镜像。新建API 28的模拟器后,自定义分辨率,高宽比最好设置为2以上,比如2280 * 1080。安卓系统默认支持的宽高比是1.7-1.8左右。具体多少值忘记了。有同学知道的在下方留言哦。但是现在出现了比值大于1.8了会出现什么情况呢?那我先给大家放一张没有做适配较大宽高比应用出现的情况的样子:

android刘海屏适配(刘海屏或圆角适配)

第一张是compileSdkVersion 为25,targetSdkVersion为22的时候的情形。第二张是compileSdkVersion 为28,targetSdkVersion为28的正常需要显示的场景,模拟器分辨率为1080 * 2280。那为何在最下方出现黑色区域呢?

正如前面说到的安卓系统默认支持的宽高比是1.7-1.8左右,现在流行的全面屏手机分辨率宽高比大于了这个数,导致屏幕区域不能利用完全,就出现了黑色区域。解决办法也很简单。

  1. 就是将编译版本compileSdkVersion升到24以上,目标版本targetSdkVersion升到24以上;
  2. 如果不愿升级targetSdkVersion,可以在AndroidManifest.xml中application节点下设置android:resizeableActivity=”true”
    <application
            android:name=".App"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:resizeableActivity="true"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:theme="@style/AppTheme">

     

  3. 或者在AndroidManifest.xml中application中设置
    <!-- Render on full screen up to screen aspect ratio of 2.4 -->
    <!-- Use a letterbox on screens larger than 2.4 -->
    <meta-data android:name="android.max_aspect" android:value="2.4" />

     

刘海屏

有刘海屏真机的同学可以跳过这部分,首先在设置中打开“开发者选项”,找到“绘图”,找到“模拟具有凹口的显示屏”,选择“长型显示屏凹口”。如图:

这里是3张图片

第二张图就是开启模拟选项后显示的切口样式,当然这个缺口是模拟的,目前真机普遍的刘海屏高度在90px左右,安卓模拟器模拟的刘海屏高度为144px。第三张图是开启一个应用的闪屏页没有适配刘海屏的高度时候的样子,顶部出现了黑色区域。这个黑色区域是因为刘海缺口导致的整个界面整体向下移。因为我们的闪屏页一般都是会全屏显示,那我们就需要适配刘海屏上的全屏样式了。

坐好了,我开始吹一波了。

 <style name="theme_loading" parent="@android:style/Theme.Light.NoTitleBar.Fullscreen">
        <item name="android:windowBackground">@drawable/loading</item>
    </style>

闪屏页的样式我设置了全屏

代码中这样判断:

 /**
     * adapt fullScreen mode
     *
     * @param mActivity a
     */
    public static void openFullScreenModel(Activity mActivity) {
        try {
            if (Build.VERSION.SDK_INT >= 28) {
                mActivity.requestWindowFeature(Window.FEATURE_NO_TITLE);
                WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
                lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
                mActivity.getWindow().setAttributes(lp);
                View decorView = mActivity.getWindow().getDecorView();
                int systemUiVisibility = decorView.getSystemUiVisibility();
                int flags = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN;
                systemUiVisibility |= flags;
                mActivity.getWindow().getDecorView().setSystemUiVisibility(systemUiVisibility);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

如果是需要全屏显示的activity,就需要调用这个方法。这样设置后我们的内容区域就可以全屏显示了。如下图:

这里有2张图片

第二张是进入的主页。

我另外又同时为大家准备了另外一个适配场景,其中做了适配和没有做适配的页面如下图:

这里是2张图片

想必大家从以上2个场景中也能总结出刘海屏的适配规则了:

就是界面的重要元素(例如按钮)不能被状态栏遮挡,需要充分的利用状态栏的显示区域。

这个也是根据场景而定,如果你的界面最顶部元素不需要侵入状态栏,那就只需要关注设置了全屏样式的activity的适配。否则你还得继续做一部分适配工作。那我们来看看显示正常的activity代码:

/**
 * Author:William Time:2018/8/12
 * Class Comment:has handled cutout
 */
public class HasAdaptActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        StatusBarManager.setStatusBar(this, true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_has_adapt);

        // adapt cutout
        int pH = DisplayCutoutUtil.getStatusBarHeight(this);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.topMargin = pH;
        findViewById(R.id.rl_top).setLayoutParams(params);
    }
}
/**
     * get statusBar height
     *
     * @param context c
     * @return h
     */
    public static int getStatusBarHeight(Context context) {
        try {
            int result = 0;
            int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                result = context.getResources().getDimensionPixelSize(resourceId);
            }
            Log.d(TAG, "getStatusBarHeight==========>" + result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".activity.NoAdaptActivity">

    <ImageView
        android:id="@+id/iv_image"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:scaleType="centerCrop"
        android:src="@drawable/image" />

    <Button
        android:id="@+id/button"
        style="@style/tv_wrapContent.txt14"
        android:layout_below="@+id/iv_image"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="保证界面按钮不被状态栏遮挡" />

    <RelativeLayout
        android:id="@+id/rl_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="按钮1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_margin="10dp"
            android:text="按钮2" />

    </RelativeLayout>
</RelativeLayout>

另外处理状态栏的工具类就不贴出来了,大家可以去文章最后的地址去下载。

代码大家一看就明白,就是给最顶部的按钮设置了一个marginTop即可,值即为状态栏的高度,通过代码设置可以直接适配所有版本。

另外附上厂商提供的适配方案:

  1. 华为官方适配方案:http://mini.eastday.com/bdmip/180411011257629.html
  2. oppo官方适配方案:https://open.oppomobile.com/wiki/doc#id=10159
  3. vivo官方适配方案:https://dev.vivo.com.cn/documentCenter/doc/103
  4. 谷歌适配指南:https://developer.android.google.cn/about/versions/pie/

结合华为,oppo,vivo 的适配文档,较为完整的处理缺口适配工具类代码如下:

package com.william.adaptnotch.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import java.lang.reflect.Method;

/**
 * 作者:William 时间:2018/7/23
 * 类说明:adapt cutout utils
 */
public class DisplayCutoutUtil {

    private static final String TAG = "DisplayCutoutUtil";

    /**
     * adapt fullScreen mode
     *
     * @param mActivity a
     */
    public static void openFullScreenModel(Activity mActivity) {
        try {
            if (needAdaptNotch(mActivity)) {
                mActivity.requestWindowFeature(Window.FEATURE_NO_TITLE);
                WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
                if (Build.VERSION.SDK_INT >= 28) {
                    lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
                }
                mActivity.getWindow().setAttributes(lp);
                View decorView = mActivity.getWindow().getDecorView();
                int systemUiVisibility = decorView.getSystemUiVisibility();
                int flags = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN;
                systemUiVisibility |= flags;
                mActivity.getWindow().getDecorView().setSystemUiVisibility(systemUiVisibility);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * need to adapt Notch screen
     *
     * @return true otherwise false
     */
    private static boolean needAdaptNotch(Context c) {
        return Build.VERSION.SDK_INT >= 28 || isHuaweiNotch(c) || isOppoNotch(c) || isVivoNotch(c);
    }

    /**
     * huawei
     *
     * @param context c
     * @return hasNotch
     */
    private static boolean isHuaweiNotch(Context context) {
        boolean ret = false;
        try {
            ClassLoader cl = context.getClassLoader();
            Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
            Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
            ret = (boolean) get.invoke(HwNotchSizeUtil);
        } catch (ClassNotFoundException e) {
            Log.e("test", "hasNotchInScreen ClassNotFoundException");
        } catch (NoSuchMethodException e) {
            Log.e("test", "hasNotchInScreen NoSuchMethodException");
        } catch (Exception e) {
            Log.e("test", "hasNotchInScreen Exception");
        }
        return ret;
    }

    /**
     * OPPO
     *
     * @param context Context
     * @return hasNotch
     */
    private static boolean isOppoNotch(Context context) {
        try {
            return context.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * VIVO
     * param:
     * 0x00000020表示是否有凹槽;
     * 0x00000008表示是否有圆角。
     *
     * @param context Context
     * @return hasNotch
     */
    private static boolean isVivoNotch(Context context) {
        boolean hasNotch = false;
        try {
            ClassLoader cl = context.getClassLoader();
            Class FtFeature = cl.loadClass("android.util.FtFeature");
            Method get = FtFeature.getMethod("isFeatureSupport");
            hasNotch = (boolean) get.invoke(FtFeature, new Object[]{0x00000020});
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hasNotch;
    }

    /**
     * get statusBar height
     *
     * @param context c
     * @return h
     */
    public static int getStatusBarHeight(Context context) {
        try {
            int result = 0;
            int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                result = context.getResources().getDimensionPixelSize(resourceId);
            }
            Log.d(TAG, "getStatusBarHeight==========>" + result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

}

OK,关于安卓P刘海屏的适配就介绍到这里了。其他关于全屏播放视频大家可以自己试试。欢迎大家留言与讨论!!!

demo 见 github下载地址:https://github.com/LuckyYangChen/AdaptNotch

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/127544.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • chrome加载慢_多线程有什么用

    chrome加载慢_多线程有什么用谷歌浏览器采用的是单线程下载,想要提高下载速度,就得采用多线程的下载方式,Chrome默认还是单线程下载,如果想要谷歌多线程下载,就要手动开启,下面听MacW小编娓娓道来,介绍如何开启Chrome多线程下载!先来看看开启前的下载速度,(同一个文件)看到了吧!默认情况下,只有左右,远没有到达带宽的上限,接下来跟着小编一起开启这个隐藏的功能,国产Chrome内核的浏览器通通适用,包括前段时间推送的新版Edge也可以。chrome://flags/#enable-parallel-downloading

    2022年10月8日
    0
  • SSD网络原理解析[通俗易懂]

    SSD网络原理解析[通俗易懂]SSD网络是继YOLO之后的one-stage目标检测网络,是为了改善YOLO网络设置的anchor设计的太过于粗糙而提出的,其设计思想主要是多尺度多长宽比的密集锚点设计和特征金字塔,下面我将详细的解析SSD网络结构SSD网络结构精简版详细版通过上面这个图,大家可以清楚的看到SSD的网络结构主要分为以下几个部分:VGG16BaseLayer ExtraFeatureLayer DetectionLayer NMS补充说明:在整个SSD网络中,其…

    2022年10月23日
    0
  • 360浏览器怎么设置截图快捷键_谷歌浏览器截图快捷键ctrl加什么

    360浏览器怎么设置截图快捷键_谷歌浏览器截图快捷键ctrl加什么360截图支持网页截图,前提是你要打开360浏览器,打开后,不管是什么界面都可以截图。一旦关闭,便不能使用。但是默认的360快捷键(ctrl+shift+X)可能与系统其他功能快捷键冲突,所以本文主

    2022年8月5日
    7
  • pycharm永久激活码2021_最新在线免费激活

    (pycharm永久激活码2021)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~S32PGH0SQB-eyJsaWNlbnNlSWQiOi…

    2022年3月26日
    55
  • FindWindow函数

    FindWindow函数FindWindow函数用来查询主窗口(子窗口不能查询),并且返回窗口句柄。函数原型:praram[in]lpClassName:以NULL结尾的字符串,如果为NULL,则查找所有与lpWindow

    2022年7月2日
    25
  • 大数据中数据采集的几种方式

    大数据中数据采集的几种方式一、采集大数据的方法1.1通过系统日志采集大数据用于系统日志采集的工具,目前使用最广泛的有:Hadoop的Chukwa、ApacheFlumeAFacebook的Scribe和LinkedIn的Kafka等。这里主要学习Flume。Flume是一个高可靠的分布式采集、聚合和传输系统,Flume支持在日志系统中定制各类数据发送方,用于收集数据,同时对数据进行简单处理,并写到诸如文本、HDFS…

    2022年5月20日
    66

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号