I've got an animation defined in an XML resource, and I want to attach that animation to a view. I know how to perform this in code manually, but I was wondering if it's possible to attach that animation to a view directly in layout declaration. Something like:
<ImageView android:animation="@anim/my_anim" />
So that animation will autoplay without any external code invocation. Is this possible with the built-in Android SDK (I'm not looking for some external library that can add this as a drop-in functionality)? If yes, how?
XML file saved at res/anim/my_overshoot_interpolator.
You can find the anim folder by navigating to the app > res > anim. Then go to the anim > right-click > New > Animation Resource File as shown in the below image.
Use AnimatedVectorDrawable You normally define animated vector drawables in three XML files: A vector drawable with the <vector> element in res/drawable/ An animated vector drawable with the <animated-vector> element in res/drawable/ One or more object animators with the <objectAnimator> element in res/animator/
The answer is no. You must attach the animation at runtime. But it's quite easy to do and should only take a few lines of code. For example, from the Android docs:
ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
spaceshipImage.startAnimation(hyperspaceJumpAnimation);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With