您的位置:首页 > 移动开发 > Android开发

如何正确实现Android启动屏画面的方法(避免白屏)

2018-02-26 11:25 801 查看

Android启动屏不正确的实现可能会导致用户长时间等待,或者可能会出现黑白屏。这里简单演示如何正确实现Android启动屏。

演示分为以下几个步骤:

  1. 在res/drawable文件夹中创建splash_background.xml文件。
  2. 编辑res/values/styles.xml
  3. 创建java/.../SplashActivity
  4. 编辑manifests/AndroidManifest.xml

1、在res/drawable文件夹中创建splash_background.xml文件

根据你的需求调整位图图像的重力和尺寸。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorPrimary"/>
<item android:gravity="center" android:width="100dp" android:height="100dp">
<bitmap
android:gravity="fill_horizontal|fill_vertical"
android:src="@drawable/logo"/>
</item>
</layer-list>

2、编辑res/values/styles.xml

这里的样式用于启动画面。 这是为了在启动屏幕时隐藏操作栏。

<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>
</style>
<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">@drawable/splash_background</item>
</style>
</resources>

3、创建java/.../SplashActivity

一旦App启动,SplashActivity将启动,然后转移到MainActivity。

package com.example.jtdan.goodSplash;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//switch from splash activity to main activity
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}

4、编辑manifests/AndroidManifest.xml

在清单文件中添加新的启动画面Activity。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jtdan.goodSplash">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="goodSplash"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.example.jtdan.goodSplash.SplashActivity" android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.jtdan.goodSplash.MainActivity"></activity>
</application>
</manifest>

示例源码地址:https://github.com/mrjoedang/goodSplash

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android 启动屏