How to restart android AnimatedVectorDrawables animations?

后端 未结 3 1174
梦谈多话
梦谈多话 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:26

    The official and working answer is here: https://issuetracker.google.com/issues/64591234

    This code works with >= API 16 (probably also 14-15). I'm using support library 26.1.0 and vectorDrawables.useSupportLibrary = true (so I can refer to a vector drawable in xml without crashing)

    animatedVector = AnimatedVectorDrawableCompat.create(getContext(), R.drawable.animated_clock);
    ringingAlarmImage.setImageDrawable(animatedVector);
    final Handler mainHandler = new Handler(Looper.getMainLooper());
    animatedVector.registerAnimationCallback(new Animatable2Compat.AnimationCallback() {
        @Override
        public void onAnimationEnd(final Drawable drawable) {
            mainHandler.post(new Runnable() {
                @Override
                public void run() {
                    animatedVector.start();
                }
            });
        }
    });
    animatedVector.start();
    

提交回复
热议问题