标签:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter = "false"
android:zAdjustment="bottom"
>
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="4000"
/>
</set>
Animation anim = AnimationUtils.loadAnimation(mContext, R.anim.rotate);
//监听动画的状态(开始,结束)
anim.setAnimationListener(new EffectAnimationListener());
textWidget = (TextView)findViewById(R.id.text_widget);
textWidget.setText(" 画面转移旋转动画效果");
textWidget.startAnimation(anim);
<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true"
>
<item android:drawable="@drawable/p1" android:duration="1000"></item>
<item android:drawable="@drawable/p2" android:duration="1000"></item>
<item android:drawable="@drawable/p3" android:duration="1000"></item>
<item android:drawable="@drawable/p4" android:duration="1000"></item>
<item android:drawable="@drawable/p5" android:duration="1000"></item>
<item android:drawable="@drawable/p6" android:duration="1000"></item>
</animation-list>
AnimationDrawable anim = (AnimationDrawable)getResources().
getDrawable(R.drawable.frame);
CustomAnimDrawable cusAnim = new CustomAnimDrawable(anim);
cusAnim.setAnimationListener(new FrameAnimationListener());
textWidget = (TextView)findViewById(R.id.text_widget);
textWidget.setText(" 画面逐帧动画效果");
textWidget.setBackgroundDrawable(anim);
cusAnim.start();
public class CustomAnimDrawable extends AnimationDrawable {
private final String TAG = "xmp";
private AnimationDrawable mOriAnim;
private AnimationDrawable mSelf;
private Handler mHandler;
private boolean mStarted;
private AnimEndListenerRunnable mEndRunnable;
private AnimationDrawableListener mListener;
public CustomAnimDrawable(AnimationDrawable anim) {
mOriAnim = anim;
initialize();
}
private void initialize() {
mSelf = this;
mStarted = false;
mHandler = new Handler();
mEndRunnable = new AnimEndListenerRunnable();
for (int i = 0; i < mOriAnim.getNumberOfFrames(); i++) {
mSelf.addFrame(mOriAnim.getFrame(i), mOriAnim.getDuration(i));
}
}
@Override
public void start() {
mOriAnim.start();
mStarted = true;
mHandler.post(mEndRunnable);
if (mListener != null) {
mListener.onAnimationStart(mSelf);
}
Log.v(TAG, "------CustomAnimDrawable------>start");
}
/**
* 循环检测 动画的状态
*/
class AnimEndListenerRunnable implements Runnable {
@Override
public void run() {
// 动画已开始
if (!mStarted) {
return;
}
// 未停止继续监听
if (!isEnd()) {
//这里的延迟时间是跟你的每一帧动画时间有关,基本保持一致就可以,最后一帧也会完整播放
//上面动画时间为每一帧1000ms,所以这里设为了1000ms
mHandler.postDelayed(mEndRunnable,1000);
return;
}
Log.v(TAG, "----------->over");
// 动画已结束
if (mListener != null) {
mStarted = false;
mListener.onAnimationEnd(mSelf);
}
}
}
/**
* 判断动画是否结束 采用反射机制
* @return
*/
private boolean isEnd(){
Class<AnimationDrawable> animClass = AnimationDrawable.class;
try{
//利用Java反射方法判断是否结束
//获得私有方法 例如
//Method method = animClass.getDeclaredMethod("nextFrame",boolean.class);
//访问其私有变量
Field field = animClass.getDeclaredField("mCurFrame");
field.setAccessible(true);
int currFrameNum = field.getInt(mOriAnim);
int totalFrameNum = mOriAnim.getNumberOfFrames();
if((currFrameNum == totalFrameNum - 1)||
(currFrameNum == -1)){
return true;
}
}
catch (Exception e) {
Log.v(TAG,"-------->Exception");
}
return false;
}
public void setAnimationListener(AnimationDrawableListener listener) {
mListener = listener;
}
public interface AnimationDrawableListener {
/**
* Notifies the start of the animation
* @param animation
*/
public void onAnimationStart(AnimationDrawable animation);
/**
* Notifies the end of the animation
* @param animation
*/
public void onAnimationEnd(AnimationDrawable animation);
}
}
标签:
原文地址:http://www.cnblogs.com/zhuzhenfeng/p/4745820.html