首页 > 代码库 > Android 利用TimerTask实现ImageView图片播放效果

Android 利用TimerTask实现ImageView图片播放效果

在项目开发中,往往 要用到图片播放的效果,今天就用TimerTask和ImageView是实现简单的图片播放效果。

其中,TimerTask和Timer结合一起使用,主要是利用TimerTask的迭代延时等时间段处理事件的机制。

具体实例如下:

1.layout xml代码

<span style="font-family:Microsoft YaHei;font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

    <!-- 开始播放 -->
    <Button
        android:id="@+id/my_start_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start" />
    
    <!-- 停止播放 -->
    <Button
        android:id="@+id/my_stop_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Stop" />
    
    <!-- 重新开始播放 -->
    <Button
        android:id="@+id/my_restart_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Restart" />

    <ImageView
        android:id="@+id/image_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src=http://www.mamicode.com/"@drawable/onea" />>
2.MainActivity代码

<span style="font-family:Microsoft YaHei;font-size:18px;">package com.example.myimageplaydemo;

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.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener {

	private Button startBtn, stopBtn,restartBtn;
	private ImageView imageIv;
	private Timer timer;
	private TimerTask timerTask;
	private int count = 0;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			int myCount = Integer.valueOf(msg.obj.toString());

			switch (msg.what) {
			case 1:
				setImageViewSrc(myCount);
				break;
			case 2:
				stopTimer();
				break;
			case 3:
				setImageViewSrc(myCount);
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public void onStart() {
		super.onStart();

		startBtn = (Button) this.findViewById(R.id.my_start_btn);
		stopBtn = (Button) this.findViewById(R.id.my_stop_btn);
		restartBtn = (Button) this.findViewById(R.id.my_restart_btn);
		imageIv = (ImageView) this.findViewById(R.id.image_iv);

		startBtn.setOnClickListener(this);
		stopBtn.setOnClickListener(this);
		restartBtn.setOnClickListener(this);
	}

	@Override
	public void onStop() {
		super.onStop();
		stopTimer();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.my_start_btn:
			if (timerTask == null) {
				timer = new Timer();
				//延迟一秒,迭代一秒设置图片
				timerTask = new TimerTask() {

					@Override
					public void run() {
						++count;
						handler.sendMessage(handler.obtainMessage(1, count));
					}
				};
				timer.schedule(timerTask, 1000, 1000);
			} else {
				handler.sendMessage(handler.obtainMessage(1, count));
			}
			break;
		case R.id.my_stop_btn:
			handler.sendMessage(handler.obtainMessage(2, count));
			break;
		case R.id.my_restart_btn:
			if (timerTask == null) {
				count = 0 ;
				timer = new Timer();
				timerTask = new TimerTask() {

					@Override
					public void run() {
						count++;
						handler.sendMessage(handler.obtainMessage(3, count));
					}
				};
				timer.schedule(timerTask, 1000, 1000);
			} else {
				handler.sendMessage(handler.obtainMessage(3, 0));
			}
		}
	}

	/**
	 * 根据count循环对ImageView设置图片
	 * @param count
	 */
	private void setImageViewSrc(int count) {
		int myCount = count % 7;
		switch (myCount) {
		case 0:
			imageIv.setImageResource(R.drawable.onea);
			break;
		case 1:
			imageIv.setImageResource(R.drawable.oneb);
			break;
		case 2:
			imageIv.setImageResource(R.drawable.onec);
			break;
		case 3:
			imageIv.setImageResource(R.drawable.oned);
			break;
		case 4:
			imageIv.setImageResource(R.drawable.onee);
			break;
		case 5:
			imageIv.setImageResource(R.drawable.onef);
			break;
		case 6:
			imageIv.setImageResource(R.drawable.oneg);
			break;
		}
	}
	
	/**
	 * 销毁TimerTask和Timer
	 */
	private void stopTimer(){
		if (timerTask != null) {
			timerTask.cancel();
			timerTask = null;
		}
		
		if(timer != null){
			timer.cancel();
			timer = null;
		}
	}

}
</span>

其中
<span style="font-family:Microsoft YaHei;font-size:18px;"><span style="white-space:pre">			</span>if (timerTask == null) {
				timer = new Timer();
				//延迟一秒,迭代一秒设置图片
				timerTask = new TimerTask() {

					@Override
					public void run() {
						++count;
						handler.sendMessage(handler.obtainMessage(1, count));
					}
				};
				timer.schedule(timerTask, 1000, 1000);
			} else {
				handler.sendMessage(handler.obtainMessage(1, count));
			}</span>
timerTask延迟一秒后再每秒设置不一样的图片,根据count进行循环的播放。

源码地址:http://download.csdn.net/detail/a123demi/7736643