Any tips for reducing lag while running multiple animations at the same time?

As soon as my application reaches ~ 4+ animations running at the same time, the animations start to lag a bit. Is there any way that I can fix / optimize this? I am using ObjectAnimator and ValueAnimator.

+4
source share
2 answers

So, if the views should not be redrawn during the animation, you can enable the hardware levels during the animation. For instance:

final View myView = // your view
Animator animator = ObjectAnimator.ofFloat(myView, View.ALPHA, 0f, 1f);
animator.setDuration(1000);
animator.addAnimatorListener(new Animator.AnimatorListener() {
    @Override
    public void onAnimationStart(Animator animator) {
        myView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    }

    @Override
    public void onAnimationCancel(Animator animator) {
        myView.setLayerType(View.LAYER_TYPE_NONE, null);
    }

    @Override
    public void onAnimationEnd(Animator animator) {
        // See http://stackoverflow.com/a/19615205/321697
        myView.post(new Runnable() {
            @Override
            public void run() {
                myView.setLayerType(View.LAYER_TYPE_NONE, null);
            }
        }
    }
}
animator.start();

, , , ( , , , , ), .

onAnimationEnd , 4.0.x, none onAnimationEnd. 4.1 , .

. , , , , , .

+4

ViewPropertyAnimator, . ( ) 52 imageViews ( ) . , ViewPropertyAnimator.

0

All Articles