Android Image view animation is slow

2019-07-09 00:18发布

问题:

I am trying to apply multiple-animation for multiple image views at the same time. Below is the code snippet.

    final AnimatorSet animationSet = new AnimatorSet();
            for(int i=0;i<frame.getChildCount();i++){
                final ImageView vw = (ImageView) frame.getChildAt(i);

                animationSet.playTogether(
                        ObjectAnimator.ofFloat(vw, "rotationY", 60),
                        ObjectAnimator.ofFloat(vw, "scaleY", 0.8f),
                        ObjectAnimator.ofFloat(vw, "x", 0)
                );
                animationSet.setDuration(600);
                animationSet.setInterpolator(new DecelerateInterpolator(1.5f));
                animationSet.start();

this is applied to every item while swiping.

When i swipe on the screen images will animate. This animation is very slow when the image count increases. I have tried providing below

1.Adding Layer type and setting null while animation ended

2.Tried Setting hardwareaccelerated aattribute in Manifest

3.Tried setting large heap attribute in manifest

4.Added animation cache for items.

5.Tried running with high RAM devices.

is there any option to improve animation performance or any alternatives in android.?

Thanks in advance.

回答1:

Use ViewPropertyAnimator . Its probably the easiest way to animate Views and is usually pretty smooth. For your example it would be something like this :

vw.animate()
    .rotationY(60)
    .scaleY(0.8f)
    .setDuration(600)
    .setInterpolator(new DecelerateInterpolator());

Check the documentation for all available methods.

EDIT : If multiple Animations at the same time are lagging, try using a Handler it will take care of the Threading for you, and should make it smoother. Something like this :

private Handler mHandler = new Handler();

for(int i=0;i<frame.getChildCount();i++){
    final ImageView vw = (ImageView) frame.getChildAt(i);

     mHandler.post(new Runnable() {
       @Override
       public void run () {
           vw.animate()
                .rotationY(60)
                .scaleY(0.8f)
                .setDuration(600)
                .setInterpolator(new DecelerateInterpolator());

           }
       });
    }