Android 利用TimerTask實現ImageView圖片播放效果,

來源:互聯網
上載者:User

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="@drawable/onea" /></LinearLayout></span>

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() {@Overridepublic 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;}}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overridepublic 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);}@Overridepublic void onStop() {super.onStop();stopTimer();}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.my_start_btn:if (timerTask == null) {timer = new Timer();//延遲一秒,迭代一秒設定圖片timerTask = new TimerTask() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.