逻辑:单页启动页,一般为一张图片,两种进入主页面方式。
方式一:在启动页面等待2秒,自动进入主页面;方式二:点击启动页,直接进入主页面。
首先,布局不用多说,就一个ImageView。
activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_splash"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/lauch_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/splash"/>
</RelativeLayout>
然后,activity逻辑处理:
1.隐藏顶部状态栏
getWindow().getDecorView().setSystemUiVisibility(View.INVISIBLE);
2.方式一延时2s进入主页面,2s延时执行在子线程用handler
3.点击进入主页面,主页面activity改为单例模式,方式多次点击多次跳转,在AndroidManifest.xml文件,注册splashActivity时设置。还有也可不改为单例模式,定义一个boolean变量,当为true时,跳转,而后改为false,再次判断时就不在跳转
<activity android:name=".activity.SplashActivity" android:launchMode="singleTask">
SplashActivity中
/**
* 跳转到主页面,并且把当前页面关闭掉
*/
private void startMainActivity() {
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
//关闭当前页面
finish();
}
或者
private boolean isStartMain = false;
/**
* 跳转到主页面,并且把当前页面关闭掉
*/
private void startMainActivity() {
if(!isStartMain){
isStartMain = true;
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
//关闭当前页面
finish();
}
}
4.跳入主页面后handler把所有的消息移除,也是为了防止多次跳入主页面
@Override
protected void onDestroy() {
//把所有的消息和回调移除
handler.removeCallbacksAndMessages(null);
super.onDestroy();
}
5.跳入主页面之后销毁SplashActivity,主页面返回键不能再跳入启动页面
private void startMainActivity() {
if(!isStartMain){
isStartMain = true;
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
//关闭当前页面
finish();
}
}
总结:SplashActivity
package com.atguigu.mobileplayer2.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import com.atguigu.mobileplayer2.R;
public class SplashActivity extends Activity {
private static final String TAG = SplashActivity.class.getSimpleName();//"SplashActivity"
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
getWindow().getDecorView().setSystemUiVisibility(View.INVISIBLE);
handler.postDelayed(new Runnable() {
@Override
public void run() {
//两秒后才执行到这里
//执行在主线程中
startMainActivity();
Log.e(TAG, "当前线程名称==" + Thread.currentThread().getName());
}
}, 2000);
}
private boolean isStartMain = false;
/**
* 跳转到主页面,并且把当前页面关闭掉
*/
private void startMainActivity() {
if(!isStartMain){
isStartMain = true;
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
//关闭当前页面
finish();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.e(TAG,"onTouchEvent==Action"+event.getAction());
//点击页面直接进入,不再需要等待2秒
startMainActivity();
return super.onTouchEvent(event);
}
@Override
protected void onDestroy() {
//把所有的消息和回调移除
handler.removeCallbacksAndMessages(null);
super.onDestroy();
}
}
可直接应用
效果