如何正確實(shí)現(xiàn)Android啟動屏畫面的方法(避免白屏)
Android啟動屏不正確的實(shí)現(xiàn)可能會導(dǎo)致用戶長時間等待,或者可能會出現(xiàn)黑白屏。這里簡單演示如何正確實(shí)現(xiàn)Android啟動屏。
演示分為以下幾個步驟:
- 在res/drawable文件夾中創(chuàng)建splash_background.xml文件。
- 編輯res/values/styles.xml
- 創(chuàng)建java/.../SplashActivity
- 編輯manifests/AndroidManifest.xml
1、在res/drawable文件夾中創(chuàng)建splash_background.xml文件
根據(jù)你的需求調(diào)整位圖圖像的重力和尺寸。
<?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、創(chuàng)建java/.../SplashActivity
一旦App啟動,SplashActivity將啟動,然后轉(zhuǎn)移到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
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 獲取drawable目錄圖片 并存入指定文件的步驟詳解
這篇文章主要介紹了Android 獲取drawable目錄圖片 并存入指定文件,本文分步驟通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
Android Studio里如何使用lambda表達(dá)式
這篇文章主要介紹了Android Studio里如何使用lambda表達(dá)式,需要的朋友可以參考下2017-05-05
淺談Android PathMeasure詳解和應(yīng)用
本篇文章主要介紹了淺談Android PathMeasure詳解和應(yīng)用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
Android操作系統(tǒng)的架構(gòu)設(shè)計(jì)分析
這篇文章主要介紹了Android操作系統(tǒng)的架構(gòu)設(shè)計(jì)分析,Android系統(tǒng)架構(gòu)分為Linux內(nèi)核驅(qū)動、C/C ++框架、Java框架、Java應(yīng)用程序,本文分別講解了它的作用,需要的朋友可以參考下2015-06-06
Android開發(fā)之判斷有無虛擬按鍵(導(dǎo)航欄)的實(shí)例
下面小編就為大家分享一篇Android開發(fā)之判斷有無虛擬按鍵(導(dǎo)航欄)的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Android實(shí)現(xiàn)城市選擇三級聯(lián)動
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)城市選擇三級聯(lián)動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12

