首先,定義一個rotate的animation:
Xml代碼
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromDegrees="0"
android:toDegrees="+360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000" />
</set>
在code裡面,實例化一個freshThrad去畫animation。點擊stop的時候,call freshThrad.interrupt();將此時wait的freshThrad喚醒,跳用imagview的clearAnimation方法,停止動畫。
Java代碼
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(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.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);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(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(){
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
icon.startAnimation(animation);
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
});
freshThrad = new MyThread();
freshThrad.start();
}
class MyThread extends Thread {
@Override
public synchronized void run() {
// TODO Auto-generated method stub
Message msg1 = new Message();
msg1.what = START_ANIMATION;
mHandler.sendMessage(msg1);
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
Log.d("debug", "stop therad");
Message msg2 = new Message();
msg2.what = STOP_ANIMATION;
mHandler.sendMessage(msg2);
e.printStackTrace();
}
}
}
}
電擊start之後,重新起thread開始動畫。
作者“Terry's Note Event”