Rotating Image Animation on the ImageView

This post is about rotating the image on the ImageView, which is useful for apps using animations. This can be a good image rotation animation to make an image automatically rotate without using any touch which is used in Android App animation these days.

NOTE :  This project is developed in Android Studio, and tested in Android Marshmallow 6.0 .

Follow the steps below to create an rotate an image automatically:

  • Create a new project with any minimum SDK of your choice.
  • Copy and paste an image which you want to have on your ImageView in the drawable folder (I named the image image.jpg)image
  • Create an ImageView in the activity_main.xml and display the image. See the following code to create the imageview.
    <?xml version="1.0" encoding="utf-8"?>
    <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"
        android:orientation="vertical"
        android:id="@+id/screen"
        tools:context=".MainActivity" >
    
    
        <ImageView
            android:id="@+id/icon"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/image" />
    
    </RelativeLayout>
  • In res folder, Create New Directory with name anim.
  • In anim directory, Create a new XML file with name rotate and add the following code in itrotate

 

<?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:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="500"
        android:repeatCount="8"
        />
    <rotate
        android:startOffset="500"
        android:fromDegrees="360"
        android:toDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="500" >
    </rotate>

</set>

  • Now, add the final code in your main file i.e MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

    ImageView icon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        icon = (ImageView) findViewById(R.id.icon);


        Animation animRotateIn_icon = AnimationUtils.loadAnimation(this, R.anim.rotate);
        icon.startAnimation(animRotateIn_icon);


    }
}


4 thoughts on “Rotating Image Animation on the ImageView

    1. Thank You Buddy!!!!! more unique and informative posts are yet to come. Meanwhile, you can add any of your post,ask queries,answer queries to help people reach out the information on http://www.sachinkumar.me/questions/. Also, you can subscribe our newsletter so you do not miss any information.

  1. I have been exploring for a little bit for any high quality articles or weblog posts on this
    kind of area . Exploring in Yahoo I ultimately
    stumbled upon this website. Studying this info So i’m satisfied to express that I have a very good uncanny feeling I found out just what I needed.
    I such a lot definitely will make sure to don?t
    put out of your mind this site and provides it a glance regularly.

  2. We really appreciate your acknowledgement!!! more unique and informative posts are yet to come. Meanwhile, you can add any of your post,ask queries,answer queries to help people reach out the information on http://www.sachinkumar.me/questions/. Also, you can subscribe our newsletter so you do not miss any information.

Leave a Reply

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

2 × four =