Android imageview image rotate animation example programmatically

Start imageview image rotation animation on button click dynamically through coding.

Sometimes application developer need to implement simple image animation on android app mostly on child education and entertainment apps. So here is the complete step by step tutorial for Android imageview image rotate animation example programmatically.

android-project-download-code-button

List of files used in this project :

  • MainActivity.java programming file.
  • activity_main.xml layout file.
  • rotation_animation.xml layout file .

Note: Copy the image inside drawable-hdpi folder.

drawable hdpi folder

Download project sample image from below and put it into drawable-hdpi folder.

sample_animation_image

Android imageview image rotate animation example programmatically.

Code for MainActivity.java file.

package com.imageviewrotationanimation_android_examples.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

 ImageView AnimationImage;
 Button AnimationButton;
 Animation animation;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 AnimationButton = (Button)findViewById(R.id.button1);
 AnimationImage = (ImageView)findViewById(R.id.imageView1);
 
 animation = AnimationUtils.loadAnimation(MainActivity.this,R.layout.rotation_animation);
 
 AnimationButton.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 AnimationImage.startAnimation(animation);
 
 }
 });
 
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.imageviewrotationanimation_android_examples.com.MainActivity" >

 <ImageView
 android:id="@+id/imageView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="62dp"
 android:src="@drawable/sample_animation_image" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/imageView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="35dp"
 android:text="Click here to start image rotation animation" />

</RelativeLayout>

Code for MainActivity.java file.

 <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:interpolator="@android:anim/linear_interpolator">
 <rotate
 android:toDegrees="360"
 android:duration="3000"
 android:fromDegrees="0"
 android:pivotX="50%"
 android:pivotY="50%"
 android:startOffset="0"
 android:repeatCount="100"/>
</set>

Screenshots:

Android imageview image rotate animation example programmatically

image_animation_3

image_animation_2

Click here to download Android imageview image rotate animation example programmatically project.

Leave a Reply

Your email address will not be published. Required fields are marked *