Cannot cast to AnimatedVectorDrawableCompat in Nougat
Instead of making checks with API<21
you can cast to Animatable
since both AnimatedVectorDrawable
and AnimatedVectorDrawableCompat
implements it
var anim = mImageView.drawable as Animatable
anim.start()
It is a little late, but I just had the similar issue and I resolved it as follows. Maybe it will help someone.
When we are using vectorDrawables for animations, we need to do following three things:
- Create an AnimatedVectorDrawableCompat from the vector drawable resource:
val drawable: AnimatedVectorDrawableCompat? =
AnimatedVectorDrawableCompat.create(context, R.drawable.animated_vector)
- Cast this drawable to Animatable2Compat:
val animatable: Animatable2Compat = drawable as Animatable2Compat
- Now register for callback provided by the support
animatable.registerAnimationCallback(object : Animatable2Compat.AnimationCallback() {
override fun onAnimationEnd(drawable: Drawable?) {
// Put code to execute after the animation is done
}
})
This is what I did. Please feel free to comment if there is a better approach.
Short answer:
Use AnimatedVectorDrawableCompat.registerAnimationCallback
static method and it will do the job for you.
(drawable as Animatable).start()
AnimatedVectorDrawableCompat.registerAnimationCallback(
drawable,
object : Animatable2Compat.AnimationCallback() {
override fun onAnimationEnd(drawable: Drawable?) {
postOnAnimation {
(drawable as Animatable).start()
}
}
})
Long answer:
I was having the same problem when I was trying to loop an animated vector drawable. Until I found out the support library returns different classes (AnimatedVectorDrawable
and AnimatedVectorDrawableCompat
) on different SDK levels.
It was not documented anywhere except this wonderful blog post of Nick Butcher:
https://medium.com/androiddevelopers/re-animation-7869722af206
He says:
Interestingly the support library currently uses the native version on API 24+ and the compat version prior despite the class being introduced in API 21. This enables it to supply bug fixes to APIs 21–23.
In the blog post the author also suggests other methods to work around this issue like using AnimatedVectorDrawableCompat#create
method and setting the drawable in the runtime.
I recommend you to read the whole article.
Hope this helps.
I deal it like this:
public class MainActivity extends AppCompatActivity {
ImageView img;
Button show, play, stop;
AnimatedVectorDrawableCompat anim_show, anim_play, anim_stop;
Object canim_show, canim_play, canim_stop;
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.img);
show = findViewById(R.id.show);
play = findViewById(R.id.play);
stop = findViewById(R.id.stop);
if (Build.VERSION.SDK_INT < 21 ) {
anim_show = (AnimatedVectorDrawableCompat) getResources().getDrawable(R.drawable.xunfei_show_animated_vector);
anim_play = (AnimatedVectorDrawableCompat) getResources().getDrawable(R.drawable.xunfei_play_animated_vector);
anim_stop = (AnimatedVectorDrawableCompat) getResources().getDrawable(R.drawable.xunfei_stop_animated_vector);
}else{
canim_show = (AnimatedVectorDrawable) getResources().getDrawable(R.drawable.xunfei_show_animated_vector);
canim_play = (AnimatedVectorDrawable) getResources().getDrawable(R.drawable.xunfei_play_animated_vector);
canim_stop = (AnimatedVectorDrawable) getResources().getDrawable(R.drawable.xunfei_stop_animated_vector);
}
show.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onClick(View view) {
if (Build.VERSION.SDK_INT < 21) {
img.setImageDrawable(anim_show);
anim_show.start();
} else {
img.setImageDrawable((AnimatedVectorDrawable) canim_show);
((AnimatedVectorDrawable) canim_show).start();
}
}
});
}
}