当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Pause and Resume AnimatorSet Api < 19

问题描述:

i want to pause and resume a animator set(all of the animatons in the animator set run sequentially).

i have written this code ,but it doesn't work properly(doesn't resume from the same place) :

 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;

}

}

}

网友答案:

Assuming the animations in the AnimatorSet are all ValueAnimator/ObjectAnimators, you can use ValueAnimator.getCurrentPlayTime() to track the playing position of the on-going animator in the set before you cancel it. When you resume the set, you can then call ValueAnimator.setCurrentPlayTime(long) to restore the play position of the unfinished animator in the new set. The two APIs mentioned above were both introduced in API level 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;
            }
        }
    }
分享给朋友:
您可能感兴趣的文章:
随机阅读: