2011-04-29 159 views
5

我使用以下代码创建了动画。Android动画暂停和播放问题

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

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

+0

你的问题解决了吗? – Deepak 2012-04-03 10:26:21

回答

1

既然您已经扩展了更多关于您明确想要使用的信息AnimationSet,我发现了另一个适用于您的解决方案。

示例代码:

扩展AnimationSet因为你需要以取消A类的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(); 
    } 
} 

这只是一个例。目前我没有机会自己测试,这只是为了举例。

+0

如果动画被取消,它是否仍然使用setFillAfter(true)? – pumpkee 2011-04-29 06:33:07

+0

@Pennypacker:我从来没有尝试过,但我相信调用一个'cancel()'将取消无论'动画'已定义 – Wroclai 2011-04-29 06:38:59

+0

我必须动画animatioset,而不是一个TranslateAnimation。动画集没有cancel()方法。 – 2011-04-29 10:02:08