Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to attach an animation to an object in layout XML in Android?

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?

like image 920
Can Poyrazoğlu Avatar asked Apr 14 '16 17:04

Can Poyrazoğlu


People also ask

Where is the animation XML file placed?

XML file saved at res/anim/my_overshoot_interpolator.

In which folder animation will be stored?

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.

How do you animate icons on Android?

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/


1 Answers

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);
like image 58
NoChinDeluxe Avatar answered Oct 05 '22 22:10

NoChinDeluxe