Android uses TimerTask to achieve ImageView picture playback effects

Source: Internet
Author: User

In the project development, often need to use the picture to play the effect, today uses TimerTask and ImageView is realizes the simple picture to play the effect.

Among them, TimerTask and timer are used together, mainly using the iterative delay of TimerTask to deal with the event mechanism.

The concrete examples are 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:gravi Ty= "Center_horizontal" android:orientation= "vertical" > <!--start playback-<button android:id= "@+id /my_start_btn "android:layout_width=" match_parent "android:layout_height=" Wrap_content "android:text= "Start"/> <!--stop playback-<button android:id= "@+id/my_stop_btn" Android:layout_width= "M    Atch_parent "android:layout_height=" wrap_content "android:text=" Stop "/> <!--start playback 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;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 (Mess Age msg) {int myCount = integer.valueof (msg.obj.toString ()), switch (msg.what) {case 1:SETIMAGEVIEWSRC (myCount); Case 2:stoptimer (); break;case 3:setimageviewsrc (MyCount); @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 (Tim Ertask = = null) {timer = new timer ();//delay one second, iterate one second set picture TimerTask = new TimerTask () {@Overridepublic void run () {++count;handl Er.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.S Endmessage (Handler.obtainmessage (3, Count));}; Timer.schedule (TimerTask, 1000, 1000);} else {handler.sendmessage (handler.obtainmessage (3, 0));}}} /** * Set picture on ImageView based on Count Loop * @param count */private void setimageviewsrc (int count) {int myCount = count% 7;switch (my Count) {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;}} /** * destroys timertask and timer */private void Stoptimer () {if (timertask! = null) {Timertask.cancel (); timertask = null;} if (timer! = null) {timer.cancel (); timer = null;}}} </span>

Which
<span style= "Font-family:microsoft yahei;font-size:18px;" ><span style= "White-space:pre" ></span>if (TimerTask = null) {timer = new timer ();//delay one second, Iterate one second set picture 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 Delay One second and then set a different picture per second, looping the playback according to count.

Source 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.