论坛首页 移动开发技术论坛

Splash界面简单实现

浏览 4535 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (2) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-02-21  

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
   发表时间:2012-02-24  
初级贴,支持,希望楼主多发有含量的帖子。
0 请登录后投票
   发表时间:2012-02-25  
Handler timerHandler = new Handler

sendMessage可以设置时间的.不推荐使用Timer
0 请登录后投票
   发表时间:2012-02-25  
Hanlder 可以使用 sendEmptyMessageDelayed 延时发送消息即可
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics