优化——apk启动黑白屏

2020-11-13  本文已影响0人  王志强_9380

对于比较大型的应用,启动的时候(特别是冷启动)会显示一段时间黑屏或白屏(和应用的theme有关),对用户很不友好,一般来说,简单的处理方式有以下两种

<item name="android:windowDisablePreview">true</item>
不显示启动界面

<item name="android:windowIsTranslucent">true</item>
把启动界面的背景设成透明的

但是这种处理方案有个很严重的问题,就是点了图标之后,会卡在主界面一段时间再显示应用界面,实际上因为应用加载导致的

主流大应用的处理方法
  • 自定义主题
  • 将启动activity的theme设置为自定义主题
  • 在启动activity的onCreate方法中,在super.onCreate之前将theme设置回去

先定义一个theme

<style name="LaunchTheme">
    <item name="android:windowBackground">@drawable/launch_layout</item>
    <item name="android:windowFullscreen">true</item>
    <item name="windowNoTitle">true</item>
</style>

定义启动界面显示的内容
launch_layout

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- The background color, preferably the same as your normal theme -->
    <item android:drawable="@android:color/holo_red_dark"/>

    <item android:top="150dp">
        <bitmap android:gravity="top"
            android:src="@drawable/girl" />
    </item>
</layer-list>

定义一个启动的activity

public class SplashActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
                finish();
            }
        }).start();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}

activity_splash

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <ImageView
       android:id="@+id/go"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:src="@drawable/music"
       android:layout_alignParentBottom="true"
       android:layout_centerHorizontal="true"
       android:layout_marginBottom="50dp"/>

</RelativeLayout>

将启动的activity主题设为我们的启动主题

<activity android:name="com.example.optimizes.lunchapp.SplashActivity"
    android:theme="@style/LaunchTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
上一篇 下一篇

猜你喜欢

热点阅读