android延迟执行任务(刷新按钮旋转)
16lz
2022-06-03
源码下载:http://download.csdn.net/detail/forwardyzk/7638597
在android中,有时候会进行延迟执行任务,几秒后自动的跳转的新界
面。下面以定时刷新为例,来介绍延迟执行刷新开始和结束。来介绍
三个方法:Thread,TimerTask,Handler。
activity_pre.xml
<span style="font-family:SimSun;font-size:18px;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/refresh" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:src="@drawable/refresh" /></LinearLayout></span>
MainActivity.java
<span style="font-family:SimSun;font-size:18px;">package com.example.test;import java.util.Timer;import java.util.TimerTask;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.Animation;import android.view.animation.RotateAnimation;import android.widget.ImageView;public class MainActivity extends Activity {private static String TAG = MainActivity.class.getSimpleName();private RotateAnimation animation;// 旋转动画private ImageView refresh;// 刷新图片private Handler handler;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_pre);handler = new Handler() {@Overridepublic void handleMessage(Message msg) {if (msg.what == 1) {refresh.clearAnimation();}super.handleMessage(msg);}};refresh = (ImageView)findViewById(R.id.refresh);animation =new RotateAnimation(0, 359, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);animation.setDuration(500);animation.setRepeatCount(Animation.INFINITE);animation.setRepeatMode(Animation.RESTART);// 使用线程OnClickListener listener1 = new OnClickListener() {// 方法1@Overridepublic void onClick(View arg0) {refresh.setAnimation(animation);refresh.startAnimation(animation);new Thread(new Runnable() {@Overridepublic void run() {try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}Message msg = new Message();msg.what = 1;handler.sendMessage(msg);}}).start();}};// 使用延时器OnClickListener listener2 = new OnClickListener() {@Overridepublic void onClick(View arg0) {refresh.setAnimation(animation);refresh.startAnimation(animation);// 方法2TimerTask task = new TimerTask() {public void run() {refresh.clearAnimation();}};Timer timer = new Timer();timer.schedule(task, 5000);}};// 使用消息机制OnClickListener listener3 = new OnClickListener() {@Overridepublic void onClick(View arg0) {refresh.setAnimation(animation);refresh.startAnimation(animation);// 方法3new Handler().postDelayed(new Runnable() {@Overridepublic void run() {// 延迟执行的代码refresh.clearAnimation();// 清楚动画,停止动画}}, 5000);// 5000毫秒执行,5秒}};// refresh.setOnClickListener(listener1);// refresh.setOnClickListener(listener2);refresh.setOnClickListener(listener2);}}</span>
可以根据自己的喜好和使用方便程度,确定使用那种方法,
我建议使用第三种方法,这种方便最简便
更多相关文章
- [置顶] Android如何使用Https
- Android——属性动画(Property Animation)
- Android(安卓)使用模拟位置(支持Android(安卓)6.0)
- android 访问网络不能在主线程中进行以及在线程中操作UI的解决方
- 浅谈Java中Collections.sort对List排序的两种方法
- 箭头函数的基础使用
- NPM 和webpack 的基础使用
- Python list sort方法的具体使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程