Android uses TimerTask to implement ImageView image playback,

Source: Internet
Author: User

Android uses TimerTask to implement ImageView image playback,

In project development, image playback is often used. Today, TimerTask and ImageView are used to achieve simple image playback.

TimerTask and Timer are used in combination to process events in time periods such as iteration latency of TimerTask.

The specific example is as follows:

1. layout xml Code

<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"> <! -- Start playing --> <Button android: id = "@ + id/my_start_btn" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "Start"/> <! -- Stop playing --> <Button android: id = "@ + id/my_stop_btn" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "Stop"/> <! -- Start playing again --> <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 code

<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; imp Ort 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) {c Ase 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 (ti MerTask = null) {timer = new Timer (); // set the image timerTask = new TimerTask () {@ Overridepublic void run () {++ count for one second of iteration; handler. sendMessage (handler. obtainMessage (1, count) ;}}; timer. schedule (timerTask, 1000,100 0);} 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,100 0);} else {handler. sendMessage (handler. obtainMessage (3, 0) ;}}/ *** sets the image * @ param count */private void setImageViewSrc (int count) based on the count Loop) {int myCount = count % 7; switch (myCount) {case 0: ima GeIv. 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;}/*** destroy TimerTask and Timer */Private void stopTimer () {if (timerTask! = Null) {timerTask. cancel (); timerTask = null;} if (timer! = Null) {timer. cancel (); timer = null ;}}</span>

Where
<Span style = "font-family: Microsoft YaHei; font-size: 18px;"> <span style = "white-space: pre "> </span> if (timerTask = null) {timer = new Timer (); // sets the image timerTask = new TimerTask () for one second after iteration () {@ Overridepublic void run () {++ count; handler. sendMessage (handler. obtainMessage (1, count) ;}}; timer. schedule (timerTask, 1000,100 0);} else {handler. sendMessage (handler. obtainMessage (1, count) ;}</span>
After timerTask is delayed for one second, different images are set every second to play cyclically Based on count.

Source Code address: http://download.csdn.net/detail/a123demi/7736643





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.