How to restart android AnimatedVectorDrawables animations?

后端 未结 3 1164
梦谈多话
梦谈多话 2021-01-02 03:47

I have a kinda complex vector drawable that I want to animate. I used @RomanNurik\'s web tool to create the animation from a svg

That gives me a valid

3条回答
  •  一整个雨季
    2021-01-02 04:39

    I have the same issue so I did this:

    // setup and set animation
    AnimatedVectorDrawableCompat animatedVector = AnimatedVectorDrawableCompat.create(context, R.drawable.listening_vector_anim);
    imageView.setImageDrawable(animatedVector);
    
    // loop animation!
    new Thread(() -> {
        while (imageView.isAttachedToWindow()) {
            try {
                imageView.post(() -> animatedVector.start());
                Thread.sleep(2000); //depends on your animation duration
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
    

    This thread will die when the ImageView detach from window.

提交回复
热议问题