Monday, July 22, 2013

Rotate An Image 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);
    }
}


2 comments:

  1. how can i rotate image needle in angle... with giving values???

    ReplyDelete
  2. Thank you for sharing. All cool and have described examples. It is true as for me too old program. I just want to advise to try this http://ipiccy.com/ft/rotate-image . It is newer, and also very very best. I use it for quite some time. Come and see.

    ReplyDelete