Splash界面简单实现

本文介绍了一种使用Android中Timer和TimerTask以及Handler组件相结合的方法来简单实现应用启动时的Splash界面,并提供了完整的代码示例。

Splash界面简单实现

大家都知道,android的应用很多的开始都是有一个Splash界面,

如:


实现起来其实很简单

 

实现原理: 

 

    通过TimerTimerTask,Handler的结合。Timer来计时,TimerTask来判断是不是已经满足设定时间,hanlder来具体启动新的Activity



 

<!--EndFragment-->
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;

public class SplashActivity extends Activity {
	private long startTime;
	private boolean touched=false;
	private Timer timer ;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.splash);
		//开启 定时器
		timer = new Timer(true);
		startTime = System.currentTimeMillis();
		timer.schedule(task, 0, 1);
	}

	
	private final TimerTask task = new TimerTask() {
		public void run() {
			if (task.scheduledExecutionTime() - startTime == 2000 || touched) {
				Message message = new Message();
				message.what = 0;
				timerHandler.sendMessage(message);
				timer.cancel();
				this.cancel();
			}

		}
	};

	private final Handler timerHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				SplashActivity.this.finish();
				// 跳转到新的 activity
				Intent intent = new Intent(SplashActivity.this,TabMain.class);
				SplashActivity.this.startActivity(intent);
				
				
				break;
			}
			super.handleMessage(msg);
		}
	};

	
	/**
	 * 点击直接跳转
	 */
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			touched = true;
		}
		return true;
	}
}

 

  • 大小: 9.9 KB
  • 大小: 2.8 KB
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北漂周

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值