Android在冷啓動的時候會出現白屏現象,這種現象的處理方式一般有兩種。
1.啓動後不進入APP在桌面滯留一會兒再進入APP。這也是微信的啓動方式。完美的讓用戶以爲是手機卡了。。。。
2.絕大多數的APP都會採取閃現頁面的加載方式,這個也是歡迎界面的設置。
splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:scaleType="fitXY"
android:id="@+id/splash_image"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
這個界面我設置了一個全屏得ImageView然後在Activity中去做相應的邏輯代碼。
SplashActivity
package projectdemo.view.activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import com.example.administrator.mystudyproject.R;
import projectdemo.application.Constants;
import projectdemo.utils.SharePreferenceUtils;
/**
* Created by Administrator on 2017/6/13.
*/
public class SplashActivity extends AppCompatActivity implements IActivity{
ImageView iv;
AlphaAnimation alphaAnimation;
@Override
public void initView() {
iv = (ImageView) findViewById(R.id.splash_image);
int code= (int) SharePreferenceUtils.getData(this,"theme", Constants.DEFAULT_STYLE_THEME);
iv.setImageResource(R.mipmap.ic_launcher);
alphaAnimation = new AlphaAnimation((float)0.1,(float)1);
alphaAnimation.setDuration(2500);
iv.startAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
startActivity( new Intent(SplashActivity.this,MainActivity.class));
finish();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
@Override
public int initContentView() {
return R.layout.splash_layout;
}
@Override
public void loadData() {
}
}
這裏由於我寫了一個接口,所以所有的綁定contentView都用一個函數進行返回後綁定。這裏如果按照一般的寫法,就在onCreate中執行相應操作就可以了。
這裏面主要就是一個動畫效果,我們這裏寫的是透明度的變化,當然也有其他的動畫可以設置。
設置動畫之後,我們建立了動畫監聽器,在動畫播放完成後進入首頁面。寫了這裏之後,別忘了修改AndroidManifest中的代碼,讓SplashActivity首啓動。這樣就可以實現一個帶動畫的歡迎界面來解決白屏問題了。