android 指定一段时间之后再执行

今日在做仿网易新闻启动的功能,需要用到按一段时间之后,再淡入一张图片,就需要用到定时操作,我就采用Handler进行处理,如下:

下面是每隔一段时间就执行某个操作,直到关闭定时操作:

final Handler handler = new Handler();
	     Runnable runnable = new Runnable(){
	         @Override
	         public void run() {
	             // TODO Auto-generated method stub
	             // 在此处添加执行的代码
	        	 secondImage.setVisibility(View.VISIBLE);
			     secondImage.startAnimation(inAnimation);
	            handler.postDelayed(this, 150);// 150是延时时长
	         } 
	     }; 
	     handler.postDelayed(runnable, 150);// 打开定时器,执行操作
	     handler.removeCallbacks(this);// 关闭定时器处理

下面是隔一段时间后执行某个操作一次,执行完后,不再执行

final Handler handler = new Handler();
		runCount = 0;// 全局变量,用于判断是否是第一次执行
		Runnable runnable = new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				if (runCount == 1) {// 第一次执行则关闭定时执行操作
					// 在此处添加执行的代码
					secondImage.setVisibility(View.VISIBLE);
					secondImage.startAnimation(inAnimation);
					handler.removeCallbacks(this);
				}
				handler.postDelayed(this, 150);
				runCount++;
			}

		};
		handler.postDelayed(runnable, 1000);// 打开定时器,执行操作


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。