Moving Image Animation on the ImageView

This post is about moving the image on the ImageView, which is useful for apps using animations. This can be a good image blinking animation to make an image automatically move 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 for moving 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 move and add the following code in it

moving

 

<?xml version="1.0" encoding="utf-8"?>

    <set
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:interpolator="@android:anim/linear_interpolator"
      android:fillAfter="true">

        <translate
          android:fromXDelta="0%p"
          android:toXDelta="75%p"
          android:duration="800" 
          android:repeatCount="2" />

   </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 animBlinkIn_icon = AnimationUtils.loadAnimation(this, R.anim.move);
        icon.startAnimation(animMoveIn_icon);


    }
}

 

 

 

One thought on “Moving Image Animation on the ImageView

Leave a Reply

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

14 − 7 =