我使用以下代码创建了一个动画。
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 :(得分:1)
由于您已经扩展了有关您明确想要使用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();
}
}
这只是一个例子。目前我没有机会亲自测试,这只是为了示例目的。