Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ImageView Animation

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!

like image 470
cakeforcerberus Avatar asked Jan 09 '10 04:01

cakeforcerberus


People also ask

How do you animate a picture on android?

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.

What is twined animation in Android?

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.

What do you mean by Android custom animation?

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.

What is animation in Android enlist its types?

The animations are basically of three types as follows: Property Animation. View Animation. Drawable Animation.


2 Answers

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); 
like image 121
Christopher Orr Avatar answered Oct 05 '22 12:10

Christopher Orr


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.

like image 26
Matt Avatar answered Oct 05 '22 11:10

Matt