How to connect animation to an object in XML format in Android?

I have an animation defined in an XML resource and I want to attach this animation to a view. I know how to do this in code manually, but I was wondering if it is possible to attach this animation to the view directly in the layout declaration. Sort of:

<ImageView android:animation="@anim/my_anim" />

Thus, the animation will automatically start without any external code call. Is this possible with the built-in Android SDK (I'm not looking for any external library that can add this as a transition function)? If so, how?

+4
source share
1 answer

- . . , . , Android-:

ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
spaceshipImage.startAnimation(hyperspaceJumpAnimation);
+5

All Articles