当我不得不为图像使用动画时,我准备了帧。我在这个类中使用了 AnimatioDrawable:
* This class is a hack to simply get an on finish callback for an
* AnimationDrawable.
*
*/
public class CustomAnimationDrawable extends AnimationDrawable {
// We need to keep our own frame count because mCurFrame in AnimationDrawable is private
private int mCurFrameHack = -1;
// This is the Runnable that we will call when the animation finishes
private Runnable mCallback = null;
/*
* We override the run method in order to increment the frame count the same
* way the AnimationDrawable class does. Also, when we are on the last frame we
* schedule our callback to be called after the duration of the last frame.
*/
@Override
public void run() {
super.run();
mCurFrameHack += 1;
if (mCurFrameHack == (getNumberOfFrames() - 1) && mCallback != null) {
scheduleSelf(mCallback, SystemClock.uptimeMillis() + getDuration(mCurFrameHack));
}
}
/*
* We override this method simply to reset the frame count just as is done in
* AnimationDrawable.
*/
@Override
public void unscheduleSelf(Runnable what) {
// TODO Auto-generated method stub
super.unscheduleSelf(what);
mCurFrameHack = -1;
}
public void setOnFinishCallback(Runnable callback) {
mCallback = callback;
}
public Runnable getOnFinishCallback() {
return mCallback;
}
}
在我的代码中,我像这样使用它:
CustomAnimationDrawable staticAnimation = new CustomAnimationDrawable();
//add frames with resources and duration. step is just a class of mine
staticAnimation.addFrame(getResources().getDrawable(step.getStepImageResId()), step.getStepDuration());
staticAnimation.addFrame(getResources().getDrawable(step.getStepImageResId()), step.getStepDuration());
staticAnimation.addFrame(getResources().getDrawable(step.getStepImageResId()), step.getStepDuration());
staticAnimation.setOneShot(false);
imgView.setBackgroundDrawable(staticAnimation);
staticAnimation.start();
可能不是最好的解决方案,但它满足我的需求。