Search code examples
androidanimationandroid-animationbattery-saver

How to play animations in battery saver mode in android ? Is there any work around?


I am using animation library in my app(For scanning).Whenever device goes to batter saver mode animations are being disabled.In some other apps animations are not disabled. Is there any work around to enable animations in batter saver mode ? If there is no work around then How other apps are achieving it ?


Solution

  • At last I achieved it through Count down timer.For every interval I am changing properties of view like alpha,ScaleX & ScaleY. And if I want to repeat that animation I wrapped it in a handler. I dont know whether it is best approach or not. I am posting here so that it might help someone.

    Handler mHandler = new Handler();
    
    Runnable runnable = new Runnable() {
            @Override
            public void run() {
                View view = new View(context); // View to animate
                view.setScaleX(0);
                view.setScaleY(0);
                view.setAlpha(1);
    
                CountdownTimer countDownTimer = new CountDownTimer(animationDuration, 1) {
                    @Override
                    public void onTick(long l) {
                        float value = l / 2000f;
                        view.setScaleX(1 - value);
                        view.setScaleY(1 - value);
                        view.setAlpha(value);
    
                    }
    
                    @Override
                    public void onFinish() {
                        removeView(view);
                        handler.postDelayed(runnable, 500);
                    }
                };
                countDownTimer.start();
            }
        };
        handler.postDelayed(runnable, 500);