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:
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);
}
}
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:
- Create the RotateAnimation object
- Set the Animation Properties.
- 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);
}
}
thanks, was helpful..
ReplyDeletenice
ReplyDeleteThank You !
ReplyDeleteAwesome easy way to animate
ReplyDeleteBut when I use image.setImageBitmap(bitmap) then animation doesn't work. Do you know that issue?
ReplyDeletethis constructor is more useful, I think:
ReplyDeleteRotateAnimation anim = new RotateAnimation(0f, 350f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
Many thanks for this tutorial. It made my life so much simpler
DeleteYou can also try this way without the use of button and automatic animation. try out!!
Deletehttp://www.sachinkumar.me/rotating-image-animation-on-the-imageview/
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/
ReplyDeletenice...i want to make animated splash screen can it will be available
ReplyDeletethank you for your interesting infomation. 2d animation in low cost
ReplyDelete