美文网首页性能优化
Android适配启动页白屏(避免图片icon拉伸)

Android适配启动页白屏(避免图片icon拉伸)

作者: 农田蚂蚁 | 来源:发表于2019-07-15 14:31 被阅读0次

引入

做过Splash欢迎页的都知道,一般的做法是在style中设置windowBackground为启动图,来避免冷启动时的黑屏或白屏,但是windowBackground并不能centerCrop,如果放一张尺寸的图在某些屏幕上就会出现拉伸,这种用户体验显然是很差的。

解决

1、首先,我们需要在res/drawable目录下创建一个 xml 文件,并命名为background_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:opacity="opaque">
    <!--白色矩形 作为背景色-->
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/color" />
        </shape>
    </item>
   <!--启动页面logo-->
    <item android:bottom="80dp">
        <bitmap android:src="@drawable/welcome_logo"
        android:gravity="bottom|center_horizontal"/>
    </item>
</layer-list>

2、在style.xml新建一个主题AppTheme.Launcher

<style name="AppTheme.Launcher">
        <item name="android:windowBackground">@drawable/background_splash</item>
</style>

3、然后在启动Activity设置我们刚才定义的theme

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

大功告成

相关文章

网友评论

    本文标题:Android适配启动页白屏(避免图片icon拉伸)

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