关于Android开发的那些事儿

Android Splash Screen最佳实践,包含全面屏,

2018-10-10  本文已影响1159人  Doikki

随着iPhone X的发布,一众Android厂商也开始跟风推出自己的刘海屏,全面屏手机,整个手机行业已经从传统的16:9逐渐往18:9,18.5:9等屏幕比例过渡,其中也给我们开发着带来了一个比较棘手的问题,那就是Splash Screen对全面屏手机的适配问题。市面上通用的方案就是创建适配多种分辨率的drawable文件夹,这种方案有缺点明显,不能完美的适配各种宽高比的屏幕。于是乎就有了下面这种方案就是使用layer-list来进行适配,先看效果:

16:9屏幕 全面屏不带刘海 全面屏带刘海

可以看出,无论是普通的16:9屏幕还是全面屏,刘海屏都可以完美适配。

上代码:

在drawable目录创建一个layer-list,如splash.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 背景颜色 -->
    <item android:drawable="@android:color/white" />

    <!--注意此处的bottom要和activity_splash.xml中的logo图marginBottom的值保持一致-->
    <item android:bottom="40dp">
        <!-- 图片 -->
        <bitmap
            android:gravity="center|bottom"
            android:src="@drawable/splash_logo" />
    </item>
</layer-list

在style.xml中为SplashActivity创建一个theme

<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/splash</item>
    <item name="android:windowFullscreen">true</item>
</style>

在values-v21中为SplashActivity创建一个和SplashTheme同名的一个theme

创建这个theme的主要目的是为了适配有navigation bar的手机,不让windowBackground延申到navigationBar的区域内

<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/splash</item>
    <item name="android:windowFullscreen">true</item>
    <!--不让windowBackground延申到navigation bar区域-->
    <item name="android:windowDrawsSystemBarBackgrounds">false</item>
</style>

在AndroidManifest.xml中定义SplashActivity的theme为SplashTheme

<activity
    android:name=".SplashActivity"
    android:theme="@style/SplashTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

SplashActivity布局(参考)

需注意的是logo占位图的marginBottom要与上面layer-list中的bottom属性的值保持一致,这就可视觉上和windowBackground中的logo的位置保持完全一致。使得下面的logo在任意一种屏幕下都能保持居中的位置。

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

    <ImageView
        android:id="@+id/iv_ad"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:contentDescription="广告图"
        android:scaleType="centerCrop"
        android:src="@drawable/ad_img" />


    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="40dp"
        android:layout_marginBottom="40dp"
        android:src="@drawable/splash_logo"
        android:contentDescription="logo图,占位"/>

</LinearLayout>

刘海屏适配

需解决的问题是让布局延申到刘海(状态栏)区域,方法如下:
针对Android p,在values-v28中为SplashActivity创建一个和SplashTheme同名的一个theme,

<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/splash</item>
    <item name="android:windowFullscreen">true</item>
    <!--不让windowBackground延申到navigation bar区域-->
    <item name="android:windowDrawsSystemBarBackgrounds">false</item>
    <!--适配Android P刘海屏-->
    <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
</style>

SplashActivity的onCreate中

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Window window = getWindow();
    View decorView = window.getDecorView();
    decorView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
        @TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
        @Override
        public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
            WindowInsets defaultInsets = v.onApplyWindowInsets(insets);
            return defaultInsets.replaceSystemWindowInsets(
                    defaultInsets.getSystemWindowInsetLeft(),
                    0,
                    defaultInsets.getSystemWindowInsetRight(),
                    defaultInsets.getSystemWindowInsetBottom());
        }
    });
    ViewCompat.requestApplyInsets(decorView);
    //将状态栏设成透明,如不想透明可设置其他颜色
    window.setStatusBarColor(ContextCompat.getColor(this, android.R.color.transparent));
}

华为手机
在AndroidManifest.xml中的SplashActivity的节点添加如下meta-data

<meta-data android:name="android.notch_support" android:value="true"/>

参考demo

参考资料:
Android开发文档
华为刘海屏手机安卓O版本适配指导

上一篇 下一篇

猜你喜欢

热点阅读