《转》安卓P 刘海屏的适配

安卓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月底,想必不少开发者都收到了来自各大应用市场发来的邮件。

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

 
  1. 尊敬的开发者:

  2. 您好!为保障用户的使用体验,应用市场已在3月启动Android P版本应用适配检测工作,

  3. 针对未做适配的应用开发者陆续进行邮件通知。

  4. 请于2018年7月底前务必完成Android P版本适配工作并自检通过。

  5. 针对未适配或在Android P版本体验欠佳的应用,

  6. 应用市场将在Android P版本机型上采取屏蔽或不推荐更新策略,

  7. 可能会对您的推广、用户口碑及品牌产生影响。

  8. 相关技术资料见:https://developer.android.google.cn/preview/overview.html

  9. 请各相关开发者及时调整。

这样就导致我们不得不去适配安卓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了会出现什么情况呢?那我先给大家放一张没有做适配较大宽高比应用出现的情况的样子:

第一张是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"
     
    1. <application

    2. android:name=".App"

    3. android:allowBackup="true"

    4. android:icon="@mipmap/ic_launcher"

    5. android:label="@string/app_name"

    6. android:resizeableActivity="true"

    7. android:roundIcon="@mipmap/ic_launcher_round"

    8. android:theme="@style/AppTheme">

  3. 或者在AndroidManifest.xml中application中设置
     
    1. <!-- Render on full screen up to screen aspect ratio of 2.4 -->

    2. <!-- Use a letterbox on screens larger than 2.4 -->

    3. <meta-data android:name="android.max_aspect" android:value="2.4" />

刘海屏

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

这里是3张图片

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

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

 
  1. <style name="theme_loading" parent="@android:style/Theme.Light.NoTitleBar.Fullscreen">

  2. <item name="android:windowBackground">@drawable/loading</item>

  3. </style>

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

代码中这样判断:

 
  1. /**

  2. * adapt fullScreen mode

  3. *

  4. * @param mActivity a

  5. */

  6. public static void openFullScreenModel(Activity mActivity) {

  7. try {

  8. if (Build.VERSION.SDK_INT >= 28) {

  9. mActivity.requestWindowFeature(Window.FEATURE_NO_TITLE);

  10. WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();

  11. lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;

  12. mActivity.getWindow().setAttributes(lp);

  13. View decorView = mActivity.getWindow().getDecorView();

  14. int systemUiVisibility = decorView.getSystemUiVisibility();

  15. int flags = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

  16. | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

  17. | View.SYSTEM_UI_FLAG_FULLSCREEN;

  18. systemUiVisibility |= flags;

  19. mActivity.getWindow().getDecorView().setSystemUiVisibility(systemUiVisibility);

  20. }

  21. } catch (Exception e) {

  22. e.printStackTrace();

  23. }

  24. }

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

这里有2张图片

第二张是进入的主页。

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

这里是2张图片

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

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

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

 
  1. /**

  2. * Author:William Time:2018/8/12

  3. * Class Comment:has handled cutout

  4. */

  5. public class HasAdaptActivity extends AppCompatActivity {

  6.  
  7. @Override

  8. protected void onCreate(Bundle savedInstanceState) {

  9. StatusBarManager.setStatusBar(this, true);

  10. super.onCreate(savedInstanceState);

  11. setContentView(R.layout.activity_has_adapt);

  12.  
  13. // adapt cutout

  14. int pH = DisplayCutoutUtil.getStatusBarHeight(this);

  15. RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(

  16. ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

  17. params.topMargin = pH;

  18. findViewById(R.id.rl_top).setLayoutParams(params);

  19. }

  20. }

 
  1. /**

  2. * get statusBar height

  3. *

  4. * @param context c

  5. * @return h

  6. */

  7. public static int getStatusBarHeight(Context context) {

  8. try {

  9. int result = 0;

  10. int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");

  11. if (resourceId > 0) {

  12. result = context.getResources().getDimensionPixelSize(resourceId);

  13. }

  14. Log.d(TAG, "getStatusBarHeight==========>" + result);

  15. return result;

  16. } catch (Exception e) {

  17. e.printStackTrace();

  18. }

  19. return 0;

  20. }

layout.xml

 
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. xmlns:tools="http://schemas.android.com/tools"

  4. android:layout_width="match_parent"

  5. android:layout_height="match_parent"

  6. android:orientation="vertical"

  7. tools:context=".activity.NoAdaptActivity">

  8.  
  9. <ImageView

  10. android:id="@+id/iv_image"

  11. android:layout_width="match_parent"

  12. android:layout_height="250dp"

  13. android:scaleType="centerCrop"

  14. android:src="@drawable/image" />

  15.  
  16. <Button

  17. android:id="@+id/button"

  18. style="@style/tv_wrapContent.txt14"

  19. android:layout_below="@+id/iv_image"

  20. android:layout_centerHorizontal="true"

  21. android:layout_marginTop="20dp"

  22. android:text="保证界面按钮不被状态栏遮挡" />

  23.  
  24. <RelativeLayout

  25. android:id="@+id/rl_top"

  26. android:layout_width="match_parent"

  27. android:layout_height="wrap_content">

  28.  
  29. <Button

  30. android:layout_width="wrap_content"

  31. android:layout_height="wrap_content"

  32. android:layout_margin="10dp"

  33. android:text="按钮1" />

  34.  
  35. <Button

  36. android:layout_width="wrap_content"

  37. android:layout_height="wrap_content"

  38. android:layout_alignParentRight="true"

  39. android:layout_margin="10dp"

  40. android:text="按钮2" />

  41.  
  42. </RelativeLayout>

  43. </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 的适配文档,较为完整的处理缺口适配工具类代码如下:

 
  1. package com.william.adaptnotch.utils;

  2.  
  3. import android.app.Activity;

  4. import android.content.Context;

  5. import android.os.Build;

  6. import android.util.Log;

  7. import android.view.View;

  8. import android.view.Window;

  9. import android.view.WindowManager;

  10.  
  11. import java.lang.reflect.Method;

  12.  
  13. /**

  14. * 作者:William 时间:2018/7/23

  15. * 类说明:adapt cutout utils

  16. */

  17. public class DisplayCutoutUtil {

  18.  
  19. private static final String TAG = "DisplayCutoutUtil";

  20.  
  21. /**

  22. * adapt fullScreen mode

  23. *

  24. * @param mActivity a

  25. */

  26. public static void openFullScreenModel(Activity mActivity) {

  27. try {

  28. if (needAdaptNotch(mActivity)) {

  29. mActivity.requestWindowFeature(Window.FEATURE_NO_TITLE);

  30. WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();

  31. if (Build.VERSION.SDK_INT >= 28) {

  32. lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;

  33. }

  34. mActivity.getWindow().setAttributes(lp);

  35. View decorView = mActivity.getWindow().getDecorView();

  36. int systemUiVisibility = decorView.getSystemUiVisibility();

  37. int flags = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

  38. | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

  39. | View.SYSTEM_UI_FLAG_FULLSCREEN;

  40. systemUiVisibility |= flags;

  41. mActivity.getWindow().getDecorView().setSystemUiVisibility(systemUiVisibility);

  42. }

  43. } catch (Exception e) {

  44. e.printStackTrace();

  45. }

  46. }

  47.  
  48. /**

  49. * need to adapt Notch screen

  50. *

  51. * @return true otherwise false

  52. */

  53. private static boolean needAdaptNotch(Context c) {

  54. return Build.VERSION.SDK_INT >= 28 || isHuaweiNotch(c) || isOppoNotch(c) || isVivoNotch(c);

  55. }

  56.  
  57. /**

  58. * huawei

  59. *

  60. * @param context c

  61. * @return hasNotch

  62. */

  63. private static boolean isHuaweiNotch(Context context) {

  64. boolean ret = false;

  65. try {

  66. ClassLoader cl = context.getClassLoader();

  67. Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");

  68. Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");

  69. ret = (boolean) get.invoke(HwNotchSizeUtil);

  70. } catch (ClassNotFoundException e) {

  71. Log.e("test", "hasNotchInScreen ClassNotFoundException");

  72. } catch (NoSuchMethodException e) {

  73. Log.e("test", "hasNotchInScreen NoSuchMethodException");

  74. } catch (Exception e) {

  75. Log.e("test", "hasNotchInScreen Exception");

  76. }

  77. return ret;

  78. }

  79.  
  80. /**

  81. * OPPO

  82. *

  83. * @param context Context

  84. * @return hasNotch

  85. */

  86. private static boolean isOppoNotch(Context context) {

  87. try {

  88. return context.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");

  89. } catch (Exception e) {

  90. e.printStackTrace();

  91. }

  92. return false;

  93. }

  94.  
  95. /**

  96. * VIVO

  97. * param:

  98. * 0x00000020表示是否有凹槽;

  99. * 0x00000008表示是否有圆角。

  100. *

  101. * @param context Context

  102. * @return hasNotch

  103. */

  104. private static boolean isVivoNotch(Context context) {

  105. boolean hasNotch = false;

  106. try {

  107. ClassLoader cl = context.getClassLoader();

  108. Class FtFeature = cl.loadClass("android.util.FtFeature");

  109. Method get = FtFeature.getMethod("isFeatureSupport");

  110. hasNotch = (boolean) get.invoke(FtFeature, new Object[]{0x00000020});

  111. } catch (Exception e) {

  112. e.printStackTrace();

  113. }

  114. return hasNotch;

  115. }

  116.  
  117. /**

  118. * get statusBar height

  119. *

  120. * @param context c

  121. * @return h

  122. */

  123. public static int getStatusBarHeight(Context context) {

  124. try {

  125. int result = 0;

  126. int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");

  127. if (resourceId > 0) {

  128. result = context.getResources().getDimensionPixelSize(resourceId);

  129. }

  130. Log.d(TAG, "getStatusBarHeight==========>" + result);

  131. return result;

  132. } catch (Exception e) {

  133. e.printStackTrace();

  134. }

  135. return 0;

  136. }

  137.  
  138.  

猜你喜欢

转载自blog.csdn.net/jackerjin123/article/details/82110041
今日推荐