启动页这个东西很简单,但是想做的完美却不容易要解决的有以下几点
1.启动不可以出现白屏与黑屏
2进入首页是否做到数据已加载完
第一种启动方式 不会出现白屏速度快
上代码
AndroidManifest.xml
<activity
android:name=".ui.activity.SplashActivity"
android:theme="@style/AppTheme_FullScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
android:theme="@style/AppTheme_FullScreen"
在这里AppTheme_FullScreen设置启动图
<style name="AppTheme_FullScreen" parent="AppTheme">
<item name="android:windowFullscreen">true</item>
<item name="android:windowBackground">@mipmap/splash</item>
</style>
Java代码
启动页SplashActivity
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Thread myThread = new Thread() {//创建子线程
@Override
public void run() {
try {
sleep(1000);//使程序休眠一秒
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
} catch (Exception e) {
e.printStackTrace();
}
}
};
myThread.start();//启动线程
}
}
现在就完美实现无白屏的启动了
这种方法是现在大部分人适用的方案
第二种方案是我现在项目中遇到的问题,首页接口太多,启动页跳转到首页会出现数据加载中的白屏,我的解决方案是放弃启动页直接启动首页, 进入首页之后先展示启动图片,完成网络请求之后或者网络失败之后让启动图消失
上代码
布局代码
<LinearLayout 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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/bg_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/splash" />
<TextView
android:gravity="center"
android:textSize="39sp"
android:text="首页数据"
android:id="@+id/bucuo"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TextView>
</LinearLayout>
首页代码
public class MainActivity extends AppCompatActivity {
ImageView bg_view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main19);
bg_view=findViewById(R.id.bg_view);
//请求首页数据
home_page_data();
}
private void home_page_data() {
addDisposable(RetrofitService.getNetService()
.getListOfMicroVideoCategories()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(response -> {
if (response.getStatusCode() == 200) {
//网络成功
bg_view.setVisibility(View.GONE);
} else {
showBindToast(response.getErrorInfo());
}
}, throwable -> {
//网络失败
bg_view.setVisibility(View.GONE);
}));
}
@Override
protected void onResume() {
super.onResume();
}
}
搞定 这样就提现了启动页的价值,让启动时间去做网络数据加载 .
这也是我暂时的想法如果您有更好的想法可以分享给我