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首启动。这样就可以实现一个带动画的欢迎界面来解决白屏问题了。