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!


Asked by: Gianna902 | Posted: 24-01-2022






Answer 1

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);

Answered by: Ada906 | Posted: 25-02-2022



Answer 2

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.

Answered by: Kelvin876 | Posted: 25-02-2022



Answer 3

You can also simply use the Rotate animation feature. That runs a specific animation, for a pre-determined amount of time, on an ImageView.

Animation rotate = AnimationUtils.loadAnimation([context], R.anim.rotate_picture);
splash.startAnimation(rotate);

Then create an animation XML file in your res/anim called rotate_picture with the content:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false">

    <rotate 
    android:fromDegrees="0"
    android:toDegrees="360"
    android:duration="5000"
    android:pivotX="50%"
    android:pivotY="50%">
</rotate>
</set>

Now unfortunately, this will only run it once. You'll need a loop somewhere to make it repeat the animation while it's waiting. I experimented a little bit and got my program stuck in infinite loops, so I'm not sure of the best way to that. EDIT: Christopher's answer provides the info on how to make it loop properly, so removing my bad suggestion about separate threads!

Answered by: Blake199 | Posted: 25-02-2022



Answer 4

One way - split you image into N rotating it slightly every time. I'd say 5 is enough. then create something like this in drawable

<animation-list   android:id="@+id/handimation" android:oneshot="false" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/progress1" android:duration="150" />
    <item android:drawable="@drawable/progress2" android:duration="150" />
    <item android:drawable="@drawable/progress3" android:duration="150" />
 </animation-list> 

code start

progress.setVisibility(View.VISIBLE);
AnimationDrawable frameAnimation = (AnimationDrawable)progress.getDrawable();
frameAnimation.setCallback(progress);
frameAnimation.setVisible(true, true);

code stop

AnimationDrawable frameAnimation = (AnimationDrawable)progress.getDrawable();
frameAnimation.stop();
frameAnimation.setCallback(null);
frameAnimation = null;
progress.setVisibility(View.GONE);

more here

Answered by: Edward172 | Posted: 25-02-2022



Answer 5

imgDics = (ImageView) v.findViewById(R.id.img_player_tab2_dics);
    imgDics.setOnClickListener(onPlayer2Click);
    anim = new RotateAnimation(0f, 360f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                            0.5f);
    anim.setInterpolator(new LinearInterpolator());
    anim.setRepeatCount(Animation.INFINITE);
    anim.setDuration(4000);

    // Start animating the image
    imgDics.startAnimation(anim);

Answered by: Marcus503 | Posted: 25-02-2022



Answer 6

Inside the element put:

android:repeatCount="infinite"

Answered by: Marcus356 | Posted: 25-02-2022



Answer 7

I have found out, that if you use the .getWidth/2 etc... that it won't work you need to get the number of pixels the image is and divide it by 2 yourself and then just type in the number for the last 2 arguments.

so say your image was a 120 pixel by 120 pixel square, ur x and y would equal 60 pixels. so in your code, you would right:

RotateAnimation anim = new RotateAnimation(0f, 350f, 60f, 60f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(700);

and now your image will pivot round its center.

Answered by: Rubie425 | Posted: 25-02-2022



Answer 8

Verified Code:

imageView.setImageResource(R.drawable.ic_arrow_up);

boolean up = true;

if (!up) { 
    up = true; 
    imageView.startAnimation(animate(up)); 
} else { 
    up = false; 
    imageView.startAnimation(animate(up)); 
}

private Animation animate(boolean up) {
    Animation anim = AnimationUtils.loadAnimation(this, up ? R.anim.rotate_up : R.anim.rotate_down);
    anim.setInterpolator(new LinearInterpolator()); // for smooth animation
    return anim;
}

drawable/ic_arrow_up.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
    <path
        android:fillColor="#3d3d3d"
        android:pathData="M7.41,15.41L12,10.83l4.59,4.58L18,14l-6,-6 -6,6z"/>
</vector>

anim/rotate_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:fillEnabled="true">
    <rotate
        android:duration="200"
        android:fromDegrees="-180"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="0" />
</set>

anim/rotate_down.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:fillEnabled="true">
    <rotate
        android:duration="200"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="180" />
</set>

Answered by: Vivian475 | Posted: 25-02-2022



Answer 9

Don't hard code image bounds. Just use:

RotateAnimation anim = new RotateAnimation( fromAngle, toAngle, imageView.getDrawable().getBounds().width()/2, imageView.getDrawable().getBounds().height()/2);

Answered by: Patrick699 | Posted: 25-02-2022



Similar questions

android imageview onClick animation

I guess this is kind of an odd question but I have tried setting onClicklistener on an ImageView and it has worked. But the problem is that the user cannot sense the click. I mean if some of u have worked on other mobile environs(like Apple iPhone) then when we click on an Image in other environs then it gives an effect on the image so that the user can understand that the image has been clicked. I have tried setti...


android imageview onClick animation

I guess this is kind of an odd question but I have tried setting onClicklistener on an ImageView and it has worked. But the problem is that the user cannot sense the click. I mean if some of u have worked on other mobile environs(like Apple iPhone) then when we click on an Image in other environs then it gives an effect on the image so that the user can understand that the image has been clicked. I have tried setti...


Android: How to start an infinite animation applied on an ImageView after the Activity with the animated View has been resumed?

I just read this: Android: How can I stop an infinite animation applied on an ImageView?, I tried it and it works perfectly, I set the animation in the onCreate() method of my Activity using AnimationUtils.loadAnimation(AppContext, animationRes), then I start the animati...


imageview - Android :: Image click animation

This is my requirement in Android . I need to display a tiny image. When the user presses the image the image should zoom in slightly and go back to it's original position, thus giving the impression that the image has been clicked. How do I go about doing this? (I tried using 2 different images (using image selector) in imageview, but both images got scaled to the view size and hence there was no animation visible)


android - Large source ImageView animation

What's the best image file format for Android in terms of memory? PNG is recommended for iOS as xCode does some magic with it.. Is it the same for Android? I'm currently developing a big app with multiple animations going on (sliding in screens, fading etc etc). All works well so far! However I have noticed the view animation where the view contains an ImageView with a (quite large) PNG as the source is a bit laggy...


Move an ImageView to different a position using animation in Android

I have several ImageViews in a RelativeLayout. When the user taps any of the ImageViews, I want the ImageView to be moved to a specified location using a subtle animation. Eg; I have initially set margins for LayoutParams associated with an ImageView as layoutparams1.setMargins(90,70,0,0); and I have then added it to the layout. When the ImageView is tapped, I'd l...


How to load an images to imageview in android animation?

I have done as follow imageView = (ImageView) findViewById(R.id.animation_iv); imageView.setImageResource(R.drawable.loadinganim); AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable(); animationDrawable.start(); I have copy image in loading.xml &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;animation-list xmlns:android="http://schemas.androi...


android - How to appear an image into an imageview with an animation

i have a layout like this: &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/white" android:gravity="center" android:orientation="vertical" &gt; &lt;LinearLayout android:id="@+id/linearLayout1" androi...


ImageView animation like the Android 4.0+ contact editor

I would like to animate my ImageView like the contact editor (the read only mode) in Android 4.0+ does (I am not sure about the version). But I don't know where to start. I tried to figure out what the source code does, but it is really complicated and I am not even sure, if I am looking at the right source code (PhotoSelectionActivity?). So, here is what I would like to do: I have an ImageView (400dp x 200dp, wid...


android - How to create ImageView animation sequence from drawable resources?

I am trying to load 11 images from the drawable resources and display them in an ImageView with a delay. Practically doing something like AnimationDrawable, since creating such results in an OutOfMemory exception and a crash. It is both reinventing the wheel and some practice but still causes the app to crash. If I just init the imgView and do imgView.setImageResource(R.drawable.pic1); the image is loaded. Whe...


Hide the ImageView after the Frame by Frame Animation stops in android

I just applied Frame by Frame animation in the image view , i just use 2 images in frame by frame main.java public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView iv = (ImageView) findViewById( R.id.imageView1); iv.setBackgroundResource(R.anim....


android - Make imageview appear above Gallery

I have a Gallery widget and 2 imageviews, one on the left of the screen and one on the right. I want the imageview and gallery to stay on the same line, but if there is an overlap, the imageview's z-index should be higher then the gallery's. The problem is the imageview appears underneath the gallery item. I've had a look in the android doc, but can't find the solution. My XML file is below (For some reason, the op...


android imageview onClick animation

I guess this is kind of an odd question but I have tried setting onClicklistener on an ImageView and it has worked. But the problem is that the user cannot sense the click. I mean if some of u have worked on other mobile environs(like Apple iPhone) then when we click on an Image in other environs then it gives an effect on the image so that the user can understand that the image has been clicked. I have tried setti...


Android: Changing an ImageView src depending on database field data

I'm quite new to Android development (started 2 days ago) and have been through a number of tutorials already. I'm building a test app from the NotePad exercise (Link to tutorial) in the Android SDK and as part of the list of notes I want to display a different image depending on the contents of a database field i've called ...


android - Rotating an ImageView within a Layout... how?

I have a layout with an image on it (embedded in an ImageView). I need to rotate the image (let's say) 90 degrees CCW. I've written code to animate the image rotating...: public class MainActivity extends Activity { private ImageView mImageView = null; private Animation mRotateAnimation = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedI...


scroll - android imageview scrolling to display full high quality image

i am working on displaying an image and placing an icon on top of it... clicking the icon will show an enlarged version of the image... though putting the imageview holding the image in a LinearLayout scales the image to the width of the dialog, the problem is that i need to display the image in a dialog but the image is very high resolution and hence is far bigger than the width of the dialog... I need to ...


android - setting picture captured by built-in camera into an ImageView

in my app i m calling built in camera for capturing picture but i want to set that picture into an image view following is the code . so what code should i add to it to set the picture into imageview. public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); startActiv...


android - how to pause and stop a changing ImageView

i have an ImageView in which the picture switches every 5s, i am trying to add a pause and resume button that can stop and restart the action. i am using a Handler, Runnable, and postDelay() for image switch, and i put the code on onResume. i am thinking about using wait and notify for the pause and resume, but that would mean creating an extra thread. so far for the thread, i have th...


bitmap - How to load an ImageView by URL in Android?

How do you use an image referenced by URL in an ImageView?


Android ImageView clickable overlays

I have a ImageView and draw some things on that view. Now I want to have the position of the click in the onClickListener. Although I think that's not really possible (storing the position in the onTouchListener is not working), I want to ask, if there is any other way to accomplish that? The goal is to have a image with some overlays, that should be clickable. I thought about AbsolutLayout, but that is dep...


android - How to set gravity (or margins) of ImageView using code?

I want to add ImageView to FrameLayout with Gravity or margins. but FramLayout and ImageView has no method about that(Actually, I can't found that). Reason that selects Framelayout is to put ImageView on ImageView. Help me plz. It is emergency for me to find solution. thx. Below is my code which help understanding my question. FrameLayout imageFrame = new FrameLayout(mContext); image...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top