安卓初始画面去黑色背景

时间:2021-12-25 16:20:27

安卓开发每次打开初始界面都会出现讨厌的黑色背景或白色背景,并且出现1-2s的延迟,现在发现一种新的方式可以迅速展示初始页面。

1.将activity的theme主题背景改为初始背景(value中的style)

<style name="AppSplash" parent="android:Theme">
        <item name="android:windowBackground">@mipmap/startpage</item>
        <item name="android:windowNoTitle">true</item>
</style>

2.在AndroidManifest中修改Activity的Theme

<activity
         android:name=".activity.InitActivity"
         android:label="@string/title_activity_init"
         android:theme="@style/AppSplash"> 
</activity>

3.在activity的onCreate中引用和处理

  @Override
    public void onCreate(final Bundle icicle) {
        super.onCreate(icicle);
        getWindow().setFormat(PixelFormat.RGBA_8888);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            // Translucent status bar
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // Translucent navigation bar
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_init);



        new Handler().postDelayed(new Runnable() {
            public void run() {
                /* Create an Intent that will start the Main WordPress Activity. */
              /*  Intent mainIntent = new Intent(splashScreen.this, wpAndroid.class);
                splashScreen.this.startActivity(mainIntent);
                splashScreen.this.finish();*/
                Intent intent = new Intent(InitActivity.this, MainActivity.class);
                ActivityUtil.getInstance().StartActivityWith(InitActivity.this, intent);
                finish();
            }
        }, 2000); //2900 for release

    }