我想暂停并恢复动画集(动画集中的所有动画都按顺序运行)。
我已经编写了这段代码,但是它不能正常工作(不是从同一个地方恢复):
AnimatorSet animatorSet;
int startPosition = 0 ;
long playTime = 0;
public void playOrResume(Marker marker, List<LatLng> positions, final LatLngInterpolator latLngInterpolator) {
TypeEvaluator<LatLng> typeEvaluator = new TypeEvaluator<LatLng>() {
@Override
public LatLng evaluate(float fraction, LatLng startValue, LatLng endValue) {
return latLngInterpolator.interpolate(fraction, startValue, endValue);
}
};
animatorSet = new AnimatorSet();
Animator previous = null;
for (int i = startPosition ; startPosition < positions.size(); i++) {
LatLng finalPosition = positions.get(i);
Property<Marker, LatLng> property = Property.of(Marker.class, LatLng.class, "position");
ObjectAnimator animator = ObjectAnimator.ofObject(marker, property, typeEvaluator, finalPosition);
if (previous != null) {
animatorSet.play(animator).after(previous);
} else {
animator.setCurrentPlayTime(playTime);
animatorSet.play(animator);
}
previous = animator;
}
animatorSet.setDuration(300);
animatorSet.start();
}
public void pause() {
ArrayList<Animator> animators = animatorSet.getChildAnimations();
for(int i= 0 ; i < animators.size() ; i++) {
Animator animator = animators.get(i);
if (animator.isStarted()){
startPosition = i;
playTime = animator.getCurrentPlayTime();
animatorSet.cancel();
break;
}
}
}发布于 2016-11-22 08:14:34
假设AnimatorSet中的动画都是值动画/对象动画,您可以在取消它之前使用ValueAnimator.getCurrentPlayTime()跟踪正在进行的动画的播放位置。当您继续设置时,您可以调用ValueAnimator.setCurrentPlayTime(long)来恢复新集中未完成动画器的播放位置。上面提到的两个API都是在API级别11中引入的。
AnimatorSet animatorSet;
int startPosition = 0 ;
long playTime = 0;
public void playOrResume(Marker marker, List<LatLng> positions, final LatLngInterpolator latLngInterpolator) {
TypeEvaluator<LatLng> typeEvaluator = new TypeEvaluator<LatLng>() {
@Override
public LatLng evaluate(float fraction, LatLng startValue, LatLng endValue) {
return latLngInterpolator.interpolate(fraction, startValue, endValue);
}
};
if (animatorSet == null) {
Animator previous = null;
for (int i = startPosition ; startPosition < positions.size(); i++) {
LatLng finalPosition = positions.get(i);
Property<Marker, LatLng> property = Property.of(Marker.class, LatLng.class, "position");
ObjectAnimator animator = ObjectAnimator.ofObject(marker, property, typeEvaluator, finalPosition);
if (previous != null) {
animatorSet.play(animator).after(previous);
} else {
animatorSet.play(animator);
}
previous = animator;
}
} else {
// Reuse the old animators so the start values are not changed.
List<Animator> anims = animatorSet.getChildAnimations().subList(startPosition, positions.size() - 1);
// Restore the play time of the first animator.
anims.get(0).setCurrentPlayTime(playTime);
animatorSet = new AnimatorSet();
animatorSet.playSequentially(anims);
}
animatorSet.setDuration(300);
animatorSet.start();
}
public void pause() {
ArrayList<Animator> animators = animatorSet.getChildAnimations();
for(int i= 0 ; i < animators.size() ; i++) {
Animator animator = animators.get(i);
if (animator.isStarted()){
startPosition = i;
playTime = animator.getCurrentPlayTime();
animatorSet.cancel();
break;
}
}
}https://stackoverflow.com/questions/40727655
复制相似问题