Tuesday, July 9, 2013

ImageView Animation in Android

In android we can animate an ImageView with different multiple ways.  One of the way is to use ImageSwitcher for animation between Images.
You can learn  Animating Image Using ImageSwitcher.

 ImageSwitcher is helpful when we have multiple images and we need to switch between Images, but if we have to rotate an Image, we have to use  RotateAnimation class.

Steps:
  1. Create the RotateAnimation object
  2. Set the Animation Properties.
  3. Start The Animation.

ImageView Animation Example

In this post I have an Image of Ball and rotating it through Top Left corner.

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:layout_marginTop="80dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:gravity="center"
            android:textColor="#000099"
            android:textSize="30dp"
            android:text="ImageView Animation Demo" />

        <ImageView
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="120dp"
            android:id="@+id/imageView1"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:src="@drawable/ball" />
   
</LinearLayout>


MainActivity .java

public class MainActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        ImageView image=(ImageView)findViewById(R.id.imageView1);
       
        // Step1 : create the  RotateAnimation object

        RotateAnimation anim = new RotateAnimation(0f, 350f, 15f, 15f);
        // Step 2:  Set the Animation properties
        anim.setInterpolator(new LinearInterpolator());
        anim.setRepeatCount(Animation.INFINITE);
        anim.setDuration(700);

        // Step 3: Start animating the image
         image.startAnimation(anim);

        // Later. if you want to  stop the animation
        // image.setAnimation(null);
    }
}


9 comments:

  1. Awesome easy way to animate

    ReplyDelete
  2. But when I use image.setImageBitmap(bitmap) then animation doesn't work. Do you know that issue?

    ReplyDelete
  3. this constructor is more useful, I think:

    RotateAnimation anim = new RotateAnimation(0f, 350f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

    ReplyDelete
    Replies
    1. Many thanks for this tutorial. It made my life so much simpler

      Delete
  4. Excellent stuff from you, man. I’ve read your things before and you are just too awesome. I adore what you have got right here. You make it entertaining and you still manage to keep it smart.This is truly a great blog thanks for sharing http://ancientolympiahotel.com/

    ReplyDelete
  5. nice...i want to make animated splash screen can it will be available

    ReplyDelete