首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >暂停并恢复AnimatorSet Api < 19

暂停并恢复AnimatorSet Api < 19
EN

Stack Overflow用户
提问于 2016-11-21 19:13:37
回答 1查看 1.1K关注 0票数 0

我想暂停并恢复动画集(动画集中的所有动画都按顺序运行)。

我已经编写了这段代码,但是它不能正常工作(不是从同一个地方恢复):

代码语言:javascript
复制
   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;
            }
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-22 08:14:34

假设AnimatorSet中的动画都是值动画/对象动画,您可以在取消它之前使用ValueAnimator.getCurrentPlayTime()跟踪正在进行的动画的播放位置。当您继续设置时,您可以调用ValueAnimator.setCurrentPlayTime(long)来恢复新集中未完成动画器的播放位置。上面提到的两个API都是在API级别11中引入的。

代码语言:javascript
复制
   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;
            }
        }
    }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40727655

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档