1 <!--SplashActivity Theme--> 2 <style name="SplashTheme" parent="AppTheme"> 3 <!-- 欢迎页背景--> 4 <item name="android:windowBackground">@drawable/bg_splash</item> 5 <item name="android:windowFullscreen">true</item> 6 <!-- <item name="android:windowIsTranslucent">true</item> --> <!-- 透明背景不要了 --> 7 </style>
1 <?xml version="1.0" encoding="utf-8"?> 2 <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 3 <!-- 背景颜色 --> 4 <item android:drawable="@color/color_ToolbarLeftItem" /> 5 6 <item> 7 <!-- 图片 --> 8 <bitmap 9 android:antialias="true" 10 android:gravity="center" 11 android:src="@drawable/ic_splash" /> 12 </item> 13 </layer-list>
SplashActivity 不要设置setContentView(R.layout.activity_main);
数据加载放在继承的Application里面,新开线程,在SplashActivity 里面调用即可,如下:
1 ackage com.johnson.kuyqi.mvp.view.activities; 2 3 import android.content.Intent; 4 import android.os.Bundle; 5 import android.os.Handler; 6 import android.support.annotation.Nullable; 7 import android.util.Log; 8 9 import com.johnson.kuyqi.app.base.BaseActivity; 10 import com.johnson.kuyqi.app.kuyqiapp.KuyqiApp; 11 12 /** 13 * Created by HelloWord on 2016/12/12. 14 */ 15 16 public class SplashActivity extends BaseActivity { 17 18 private static final int SPLASH_SHOW_TIME = 2000;// SplashActivity最小显示时间 19 private long mStartTime;// 开始时间 20 21 22 @Override 23 protected void onCreate(@Nullable Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 mStartTime = System.currentTimeMillis();//记录开始时间, 26 Log.i("tag", "mStartTime=" + mStartTime); 27 KuyqiApp.getInstance().initData(mHandler);//开始加载数据 28 } 29 30 @Override 31 protected void initFoundView() { 32 33 } 34 35 @Override 36 protected void initView() { 37 38 } 39 40 @Override 41 protected void initEvent() { 42 43 } 44 45 private Handler mHandler = new Handler() { 46 public void handleMessage(android.os.Message msg) { 47 switch (msg.what) { 48 case KuyqiApp.INIT_DATA_SCUESS:// 如果城市列表加载完毕,就发送此消息 49 long loadingTime = System.currentTimeMillis() - mStartTime;// 计算一下总共花费的时间 50 if (loadingTime < SPLASH_SHOW_TIME) {// 如果比最小显示时间还短,就延时进入MainActivity,否则直接进入 51 mHandler.postDelayed(goToMainActivity, SPLASH_SHOW_TIME - loadingTime); 52 } else { 53 mHandler.post(goToMainActivity); 54 } 55 break; 56 default: 57 break; 58 } 59 } 60 }; 61 62 /** 63 * 进入下一个Activity 64 */ 65 Runnable goToMainActivity = new Runnable() { 66 67 @Override 68 public void run() { 69 SplashActivity.this.startActivity(new Intent(SplashActivity.this, MainActivity.class)); 70 finish(); 71 } 72 }; 73 74 75 }
1 public static void initData(final Handler mHandler) { 2 3 new Thread() { 4 @Override 5 public void run() { 6 super.run(); 7 mHandler.sendEmptyMessage(INIT_DATA_SCUESS); 8 9 } 10 }.start(); 11 }