启用电池节电时如预期ValueAnimator不起作用(> = API 21)(ValueAn

2019-09-29 18:03发布

ValueAnimator似乎没有重复或播放时,电池保护程序的Android棒棒糖或更高启用了重复无限动画。

    animator = ValueAnimator.ofInt(0,timePeriods.length-1);
    animator.setInterpolator(new LinearInterpolator());
    animator.setEvaluator(new TypeEvaluator() {
        @Override
        public Object evaluate(float v, Object o, Object t1) {
            int time = 0 ;
            int timeElapsed = (int)(v*totalTime) ;
            for (int i = 0; i < timePeriods.length; i++) {
                time = time + timePeriods[i];
                if(time>=timeElapsed) return i ;
            }
            return 0 ;
        }
    });
    animator.setDuration(totalTime);
    animator.setRepeatCount(repeatCount);
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        int currentValue = -1 ;
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            int value = (Integer) valueAnimator.getAnimatedValue();
            if(value!=currentValue){
                Log.d(TAG,"Value Changed: "+currentValue+" "+value);
                currentValue = value ;
            }
        }
    });

当电池保护程序启用了动画播放动画只有一次,以后不会重复...是否有任何设置|应用程序或活动中的标志,即使在低电池模式下运行的动画师为上述动画是一个简单的整数valueanimator并且不任何进一步的耗尽电池。

文章来源: ValueAnimator doesn't work as expected when battery saver is enabled(>= API 21)