Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Animation pause and play problem

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?

like image 891
dev_android Avatar asked Nov 14 '22 00:11

dev_android


1 Answers

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.

Sample code:

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

}

In your Activity class:

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.

like image 57
Wroclai Avatar answered Dec 18 '22 22:12

Wroclai