I've created a layout with an image view and a web view. The web view is set to have a default visibility of gone. When the activity fires up it displays the image view first and when the web view has finished loading its url, it marks itself as visible and the imageview is marked as hidden.
When the imageview is shown, I would like it to rotate repeatedly just for a little added pizazz.
I have never done animations before in Android and all the posts I found when I asked the internet were not helpful; thus, I have returned to SO for help.
So if I start with this...
final ImageView splash = (ImageView)findViewById(R.id.splash);
How do I create a repeated rotate animation and apply it to the ImageView?
Thanks again!
In this, you will create the object of translate animation and that object to the startAnimation() method. Rotate animation is a subclass of Animation that controls the rotation of an object. In this you will create the object of rotate animation and that object to the startAnimation() method.
Tween animations are a specific type of animation related to rotation, sliding, and movement of an object. This article explains some common Tween animations. Now we move to the code part of this article. In this article, we will use a single Activity_main. xml.
A custom transition enables you to create an animation that is not available from any of the built-in transition classes. For example, you can define a custom transition that turns the foreground color of text and input fields to gray to indicate that the fields are disabled in the new screen.
The animations are basically of three types as follows: Property Animation. View Animation. Drawable Animation.
Use a RotateAnimation
, setting the pivot point to the centre of your image.
RotateAnimation anim = new RotateAnimation(0f, 350f, 15f, 15f); anim.setInterpolator(new LinearInterpolator()); anim.setRepeatCount(Animation.INFINITE); anim.setDuration(700); // Start animating the image final ImageView splash = (ImageView) findViewById(R.id.splash); splash.startAnimation(anim); // Later.. stop the animation splash.setAnimation(null);
How to rotate an image around its center:
ImageView view = ... //Initialize ImageView via FindViewById or programatically RotateAnimation anim = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //Setup anim with desired properties anim.setInterpolator(new LinearInterpolator()); anim.setRepeatCount(Animation.INFINITE); //Repeat animation indefinitely anim.setDuration(700); //Put desired duration per anim cycle here, in milliseconds //Start animation view.startAnimation(anim); //Later on, use view.setAnimation(null) to stop it.
This will cause the image to rotate around its center (0.5 or 50% of its width/height). I am posting this for future readers who get here from Google, as I have, and who wish to rotate the image around its center without defining said center in absolute pixels.
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