- 例如QQ的经典欢迎页面
前言
目前市面上的大多数 App 都有一个欢迎页面,有如下优点:
1. 利用展示欢迎页面的时间来加载数据
2. 展示品牌设计风格
步骤
- 创建布局文件
- 设置欢迎页面
- 延时 2S 跳转
创建布局文件
<?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"
tools:context="com.example.ethan.imtest.controller.activity.SplashActivity">
<ImageView
android:id="@+id/splashview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/atguigu_logo" />
</android.support.constraint.ConstraintLayout>
设置欢迎界面
<activity android:name=".controller.activity.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
延时 2s 跳转
使用 Handler 执行延时2s的操作
//延时两秒钟
private Handler handler=new Handler(){
public void handleMessage(Message msg) {
//如果当前activity已经退出,不对消息进行处理
if (isFinishing()){
return;
}
toMainOrLogin();
}
};
//选择进入主页面还是登录页面
protected void toMainOrLogin(){
new Thread(new Runnable() {
@Override
public void run() {
//如果登陆过,进入主界面
if (EMClient.getInstance().isLoggedInBefore()){
Intent intent = new Intent(SplashActivity.this,MainActivity.class);
startActivity(intent);
}
else {
//没有登录过,进入登录界面
Intent intent=new Intent(SplashActivity.this,LoginActivity.class);
startActivity(intent);
}
finish();
}
}).start();
}
@Override
protected void onDestroy(){
//销毁消息
super.onDestroy();
handler.removeCallbacksAndMessages(null);
}