本文介绍了Android的动画暂停和播放问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试使用下面的code创建的动画。

I have created an animation using the following code.

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);

这是工作的罚款。现在我来补充暂停和播放功能,此动画。我该怎么办呢?

It is working fine. Now I have to add pause and play feature for this animation. How can I do that?

推荐答案

既然你已经扩展方面,你明确地想用 AnimationSet ,我还发现了另一个了解更多信息解决方案,它应该为你工作。

Since you have extended with more information regarding that you explicity wanted to use AnimationSet, I have found another solution that should work for you.

样品code:

这是扩展一个类 AnimationSet ,你将需要以取消 AnimationSet

A class that extends AnimationSet as you will need in order to cancel an 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);
     }

}

在你的活动类:

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();
    }
}

这仅仅是一个例子。目前,我没有机会由我自己来测试它,这只是写例如目的。

This is just an example. At the moment I do not have opportunity to test it by myself, this was just written for example purpose.

这篇关于Android的动画暂停和播放问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-20 05:36