【Android 性能优化】应用启动优化 ( 主题背景图片设置 | 设置透明主题背景 | 设置应用启动主题背景、启动后恢复主题 )





一、 设置图片主题背景



设置一个主题背景图片 , 只是将白屏问题掩盖了 , 应用真实启动时间还是很长 ;


1 . 设置背景图片代替白屏 : APP 启动时显示一张默认图片 , 这样用显示图片替代之前的白屏或黑屏 , 在这几秒钟的时间内 , 用户全程看这张图片 , 体验效果要好于黑白屏界面 ;


2 . 主题中添加背景图片 : 在资源目录 res/values/styles.xml , 编辑自定义主题 , 在主题中设置 android:windowBackground 背景图片 ;

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- 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">@mipmap/ic_launcher</item>
    </style>

</resources>

3 . 设置该主题 : 将上述 AppTheme 主题 , 设置到 application 应用中 , 在 AndroidManifest.xml 配置文件中的 application 标签中配置 android:theme="@style/AppTheme" 属性 , 上述设置的背景图片即可生效 ;

    <!--  -->
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>




二、 设置透明主题背景



设置一个透明的主题背景 , 也是将白屏问题掩盖了 , 应用真实启动时间还是很长 , 这种错发有启动延迟的感觉 , 这是因为点击应用图标时 , 应用马上启动了 , 只是显示透明的主题代替了之前的白屏 , 几秒后才显示主界面 , 造成延迟的感官效果 ;


1 . 设置透明背景代替白屏 : APP 启动时显示透明背景 , 这样用透明背景替代之前的白屏或黑屏 , 在这几秒钟的时间内静止直到界面绘制显示 , 除了有启动延迟的影响 , 体验效果要好于黑白屏界面 ;


2 . 主题中添加背景图片 : 在资源目录 res/values/styles.xml , 编辑自定义主题 , 在主题中设置 android:windowIsTranslucent 透明背景为 true ;

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- 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">@mipmap/ic_launcher</item>-->

        <!-- 将背景设置成透明的 -->
        <item name="android:windowIsTranslucent">true</item>
    </style>

</resources>

3 . 设置该主题 : 将上述 AppTheme 主题 , 设置到 application 应用中 , 在 AndroidManifest.xml 配置文件中的 application 标签中配置 android:theme="@style/AppTheme" 属性 , 上述设置的背景图片即可生效 ;

    <!--  -->
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>




三、 设置应用启动主题背景、启动后恢复主题



1 . 应用主题设置 : 按照如上两种方法 , 设置主题背景图片 , 或设置透明主题背景 , 设置在 application 标签中 , 这是整个应用的主题 , 所有的 Activity 界面都会使用该主题 ;

扫描二维码关注公众号,回复: 11390692 查看本文章

2 . 启动界面主题设置 : 这里为 Launcher 启动界面设置单独的主题 , 界面启动加载完成之后 , 恢复成应用主题 ;


3 . 定义专门用于应用启动界面的主题资源 : AppTheme.Launcher.TranslucentAppTheme.Launcher.Picture 主题是专门设置给 Launcher 界面的 , 仅限于在启动过程中使用 ;

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- 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">@mipmap/ic_launcher</item>-->

        <!-- 将背景设置成透明的 -->
        <!--<item name="android:windowIsTranslucent">true</item>-->
    </style>

    <style name="AppTheme.Launcher.Translucent" parent="AppTheme">
        <!-- 将背景设置成透明的 -->
        <item name="android:windowIsTranslucent">true</item>
    </style>

    <style name="AppTheme.Launcher.Picture" parent="AppTheme">
        <!-- 设置应用启动时默认显示的图片 , 这样比白屏/黑屏体验好一些 -->
        <item name="android:windowBackground">@mipmap/ic_launcher</item>
    </style>

</resources>

4 . 为 Activity 界面配置主题 : 在 AndroidManifest.xml 中 , 为应用的启动界面 MainActivity.java 配置主题 , 可以为 MainActivity 设置 AppTheme.Launcher.Translucent 主题 , 也可以设置 AppTheme.Launcher.Picture 主题 ;

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="kim.hsl.rtmp">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:theme="@style/AppTheme.Launcher.Translucent"
            android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

5 . 主题恢复 : 这个 AppTheme.Launcher 主题仅限在启动的那几秒使用 , 在真正的界面中 , 是不想使用这个主题的 , 因此需要在应用启动界面加载完成后 , 再设置成别的主题 , 这里再 onCreate 方法中设置即可 ;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        /*
            此时应用首界面启动完成, 将主题恢复成其它主题
            此处也可以根据不同的设置, 为应用设置不同的主题
         */
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

猜你喜欢

转载自blog.csdn.net/han1202012/article/details/106870131
今日推荐