我使用以下代码创建了动画。

private AnimationSet rootSet = new AnimationSet(true);
private int xstart=258;
private int ystart=146;
for(; k<points.length; k++) {
  if(k==1) {
    x1 = headX(xstart);
    y1 = headY(ystart);
    _animTime = 10;
  } else {

    x1 = headX(points[k-1][0]);
    y1 = headY(points[k-1][1]);
  }
  translate = new TranslateAnimation((float)x1, (float)x2, (float)y1, (float)y2);
  translate.setDuration(_animTime);
  translate.setFillAfter(true);
  translate.setInterpolator(new AccelerateDecelerateInterpolator());
  totalAnimTime +=  _animTime;
  translate.setStartOffset(totalAnimTime);
  rootSet.addAnimation(translate);
  rootSet.setFillAfter(true);
}

imv1.startAnimation(rootSet);

一切正常。现在,我必须为此动画添加暂停和播放功能。我怎样才能做到这一点?

最佳答案

由于您已扩展了有关您明确想使用AnimationSet的更多信息,因此我找到了另一种适合您的解决方案。

样例代码:

用于扩展AnimationSet的类,以取消AnimationSet:

public class CustomAnimationSet extends AnimationSet {

     private AnimationListener mCustomAnimationSetListener;

     public CustomAnimationSet(boolean interpolator) {
          super(interpolator);
     }

     public CustomAnimationSet(Context context, AttributeSet attrs) {
          super(context, attrs);
     }

     @Override
     public void setAnimationListener(AnimationListener listener) {
          super.setAnimationListener(listener);
          mCustomAnimationSetListener = listener;
     }

     /**
      * Your cancel method....
      */
     public void cancel() {
          // Make sure you're cancelling an ongoing AnimationSet.
          if(hasStarted() && !hasEnded()) {
               if(mCustomAnimationSetListener != null) {
                    mCustomAnimationSetListener.onAnimationEnd(this);
               }
          }

          // Reset the AnimationSet's start time.
          setStartTime(Float.MIN_VALUE);
     }

}

Activity类中:
private CustomAnimationSet mAnimationSet;

// Init stuff.

@Override
public void onClick(View v) {
    switch(v.getId()) {
        case R.id.onPlayButton:
            // Might wanna add Animations before starting next time?
            mAnimationSet.start();
        case R.id.onPauseButton:
            mAnimationSet.cancel();
            mAnimationSet.reset();
    }
}

这只是一个例子。目前,我没有机会自己进行测试,这只是出于示例目的而编写的。

关于Android Animation暂停和播放问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5828423/

10-10 20:22