首页 > 程序开发 > 移动开发 > Android >

Android多线程之控制animation走走停停

2011-09-20

首先,定义一个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/accele...

首先,定义一个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&#39;s Note Event”

相关文章
最新文章
热点推荐