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
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();
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.
I am giving a few tips
Issue Number 1: android:repeatCount="0" to make it infinite. and android:repeatMode="restart" or "reverse".
Issue Number 2: Looping is there in ObjectAnimator. The above, under Number one is the same.
Issue Number 3:Listener is there on ObjectAnimator. If you add a listener you can watch onAnimationEnd or onAnimationStart.
Instead of a single file if you opt for the other alternative of 3 files as given in documentation for AnimatedVectorDrawable you can feel comfortable to handle.
The three are 1. VectorDrawable, 2. AnimatedVectorDrawable and 3.ObjectAnimator
The AnimatedVectorDrawable links the other two.
There is a good article in Ray Wenderlich's site with a rocket and a doge animation at : https://www.raywenderlich.com/173345/android-animation-tutorial-with-kotlin (with java code).
You may also read the article "Introduction to Icon animation Techniques" by Mr.Alex Lockwood at : https://www.androiddesignpatterns.com/2016/11/introduction-to-icon-animation-techniques.html The source code is also available in GitHub.