Android 啟動(dòng)頁(yè)白屏解決方案
當(dāng)我們打開(kāi)app的時(shí)候是不是會(huì)有一瞬間的白屏然后再進(jìn)入主活動(dòng),雖然這并不會(huì)造成什么不好的后果,但是感覺(jué)用戶體驗(yàn)就不是很好。像網(wǎng)易云音樂(lè)等等,打開(kāi)一瞬間就顯示了他們的loge,無(wú)縫銜接,沒(méi)有白屏,怎么做到的呢?
一開(kāi)始我的思路是這樣的??赡苁且?yàn)槲覀兊闹骰顒?dòng)邏輯太多,所以加載會(huì)變慢,導(dǎo)致顯示白屏。如果使用一個(gè)只顯示一張本地圖片的活動(dòng),那會(huì)不會(huì)就不會(huì)顯示白屏了呢。話不多說(shuō)我們嘗試一下:
Activity中的代碼:
/**
* 啟動(dòng)頁(yè),顯示傾旅的logo,停頓2秒后跳轉(zhuǎn)
*/
public class LunchActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lunch);
//開(kāi)啟子線程進(jìn)行停頓。如果在主線程停頓的話,會(huì)造成主頁(yè)面卡死,所以在子線程sleep兩秒后跳轉(zhuǎn)
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
start();
LunchActivity.this.finish();
}
}).start();
}
//跳轉(zhuǎn)到主頁(yè)面
private void start(){
Intent intent = new Intent(LunchActivity.this,MainActivity.class);
startActivity(intent);
}
}
layout中的代碼:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
android:background="#e74b37"
tools:context=".LunchActivity">
<ImageView
android:id="@+id/imageView5"
android:layout_width="80dp"
android:layout_height="80dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.31"
app:srcCompat="@drawable/icon" />
</android.support.constraint.ConstraintLayout>
這里簡(jiǎn)單指定一個(gè)imageView來(lái)顯示一張圖片。并把背景設(shè)置為橘色
最后再把啟動(dòng)頁(yè)活動(dòng)設(shè)置為主活動(dòng):
<activity android:name="com.example.qinglv.LunchActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
一切想的很好,完成后打開(kāi)一看,還是會(huì)白屏,怎么回事?
活動(dòng)的加載都是需要時(shí)間的,比較簡(jiǎn)單的活動(dòng)時(shí)間會(huì)少點(diǎn),但是以然會(huì)有一瞬間的白屏。那這個(gè)白屏到底是什么?就是每個(gè)活動(dòng)的背景。當(dāng)打開(kāi)一個(gè)活動(dòng)的時(shí)候,因?yàn)檫€沒(méi)加載出內(nèi)容,所以顯示的就只是背景,所以我們只需要,改變這個(gè)背景,設(shè)置為我們需要的一個(gè)logo照片即可。怎么設(shè)置呢?
- 背景是在主題中指定的,首先設(shè)置一個(gè)主題,把背景改成我們要的。一般和我們的啟動(dòng)頁(yè)保持一致,這樣的話就不會(huì)看起來(lái)像兩個(gè)啟動(dòng)頁(yè)一樣。也可以像網(wǎng)易云音樂(lè)那樣,背景設(shè)置成logo,但是啟動(dòng)頁(yè)是放廣告,但是這會(huì)影響用戶體驗(yàn)(為了收入打點(diǎn)廣告也是可以理解的)??创a:
在res-value-styles:
<style name="NewAppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="android:windowBackground">@color/colorPrimary</item> <item name="colorAccent">@color/colorAccent</item> </style>
重點(diǎn)是這句<item name="android:windowBackground">@color/colorPrimary</item>這里我指定的是一種顏色你們也可以指定一張圖片
- 再給啟動(dòng)頁(yè)活動(dòng)指定主題:
在:AndroidManifest:
<activity android:name="com.example.qinglv.LunchActivity"
android:theme="@style/NewAppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
重點(diǎn)是這句android:theme="@style/NewAppTheme"
然后再打開(kāi)的時(shí)候,就會(huì)發(fā)現(xiàn)不會(huì)了。原本顯示的白屏變成了我們?cè)O(shè)置好的圖片。
以上就是Android 啟動(dòng)頁(yè)白屏解決方案的詳細(xì)內(nèi)容,更多關(guān)于Android 啟動(dòng)頁(yè)白屏的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
android異步請(qǐng)求服務(wù)器數(shù)據(jù)示例
這篇文章主要介紹了android異步請(qǐng)求服務(wù)器數(shù)據(jù)示例,需要的朋友可以參考下2014-03-03
Android基于TextView實(shí)現(xiàn)跑馬燈效果
這篇文章主要為大家詳細(xì)介紹了Android基于TextView實(shí)現(xiàn)跑馬燈效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android使用ViewPager實(shí)現(xiàn)翻頁(yè)效果
這篇文章主要為大家詳細(xì)介紹了Android使用ViewPager實(shí)現(xiàn)翻頁(yè)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Android ImageView 不顯示JPEG圖片的問(wèn)題解決
本篇文章主要介紹了Android ImageView 不顯示JPEG圖片及Android Studio中如何引用圖片資源的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-05-05
Android+Html5混合開(kāi)發(fā)仿微信朋友圈
這篇文章主要為大家詳細(xì)介紹了Android+Html5混合開(kāi)發(fā)仿微信朋友圈的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android代碼實(shí)現(xiàn)新年賀卡動(dòng)畫(huà)示例詳解
這篇文章主要為大家介紹了Android代碼實(shí)現(xiàn)新年賀卡動(dòng)畫(huà)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Android實(shí)現(xiàn)登錄注冊(cè)頁(yè)面(上)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)登錄注冊(cè)頁(yè)面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
用Flutter開(kāi)發(fā)自定義Plugin的方法示例
這篇文章主要介紹了用Flutter開(kāi)發(fā)自定義Plugin的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-06-06

