Android多线程之控制animation走走停停原创文章,转载请标注出处----?首先,定义一个rotate的animation:?在c
Android多线程之控制animation走走停停
原创文章,转载请标注出处----
?
首先,定义一个rotate的animation:
?在code里面,实例化一个freshThrad去画animation。点击stop的时候,call freshThrad.interrupt();将此时wait的freshThrad唤醒,调用imagview的clearAnimation方法,停止动画。
package com.animation.test;import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.view.animation.RotateAnimation;import android.view.animation.Animation.AnimationListener;import android.widget.Button;import android.widget.ImageView;public class AnimationTest extends Activity implements OnClickListener{private ImageView icon; private Animation animation; private Button btStop; private Button btStart; private boolean runFlag = true; private MyThread freshThrad; private final static int START_ANIMATION = 100; private final static int STOP_ANIMATION = 101; Handler mHandler = new Handler(){@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubsuper.handleMessage(msg);switch(msg.what){case STOP_ANIMATION:icon.clearAnimation();break;case START_ANIMATION:icon.startAnimation(animation);break;}} }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); icon = (ImageView)findViewById(R.id.icon); btStop = (Button) findViewById(R.id.stop); btStart = (Button) findViewById(R.id.start); btStop.setOnClickListener(this); btStart.setOnClickListener(this); showAnimation(); icon.setOnClickListener(this); }@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch(v.getId()){case R.id.icon: icon.startAnimation(animation);break;case R.id.stop:freshThrad.interrupt();break;case R.id.start:showAnimation();break;}}private void showAnimation(){ animation = AnimationUtils.loadAnimation(this, R.anim.rotate_animation); animation.setAnimationListener(new AnimationListener(){@Overridepublic void onAnimationEnd(Animation animation) {// TODO Auto-generated method stubicon.startAnimation(animation);}@Overridepublic void onAnimationRepeat(Animation animation) {// TODO Auto-generated method stub}@Overridepublic void onAnimationStart(Animation animation) {// TODO Auto-generated method stub} }); freshThrad = new MyThread(); freshThrad.start();}class MyThread extends Thread {@Overridepublic synchronized void run() {// TODO Auto-generated method stubMessage msg1 = new Message();msg1.what = START_ANIMATION;mHandler.sendMessage(msg1);try {this.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blockLog.d("debug", "stop therad");Message msg2 = new Message();msg2.what = STOP_ANIMATION;mHandler.sendMessage(msg2);e.printStackTrace();}}}}?电击start之后,重新起thread开始动画。
?
