Android适配Splash页面

原始图片

一、适配背景图

1.1 直接使用图片作为android:windowBackground属性值

<resources>
    <!-- 直接在清单文件的application节点下面android:theme使用-->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowBackground">@drawable/blue_circle</item>
    </style>
</resources>

1.2 制作点9图

1.3 使用点9图片作为android:windowBackground属性值

<resources>
    <!-- 直接在清单文件的application节点下面android:theme使用-->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowBackground">@drawable/blue_circle1</item>
    </style>
</resources>

二、利用layer-list方式适配

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowBackground">@drawable/splashxml</item>
    </style>

</resources>

splashxml.xml源码如下:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:gravity="fill">
        <shape android:shape="rectangle">
            <solid android:color="#ff0" />
        </shape>
    </item>
    <item>
        <!--
        android:gravity设置bitmap图像的展示方式,居中/填充/水平居中/垂直居中等等
        android:tileMode设置图片的展示方式
        "clamp"表示按照周边进行绘制
        "repeat"重复展示图片
        "mirror"镜像展示图片
        使用这个属性可能导致无效android:gravity
              -->
        <bitmap
            android:gravity="center"
            android:src="@drawable/blue_circle"
            android:tileMode="clamp"/>
    </item>
</layer-list>

不使用 android:tileMode="clamp"的效果图如下所示:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:gravity="fill">
        <shape android:shape="rectangle">
            <solid android:color="#ff0" />
        </shape>
    </item>
    <item>
        <!--
        android:gravity设置bitmap图像的展示方式,居中/填充/水平居中/垂直居中等等
        android:tileMode设置图片的展示方式
        "clamp"表示按照周边进行绘制
        "repeat"重复展示图片
        "mirror"镜像展示图片
        使用这个属性可能导致无效android:gravity
              -->
        <bitmap
            android:gravity="center"
            android:src="@drawable/blue_circle"/>
    </item>
</layer-list>

三、总结

要想改变android:windowBackground的属性值,最佳效果是把小切图与背景图分开,再用layer-list来处理,否则很容易变形。点9图来做适配变形也只能适用于简单的图片。

DEMO源码地址

猜你喜欢

转载自blog.csdn.net/Duckdan/article/details/111873118