articles

Home / DeveloperSection / Articles / How to animate image left to right in android

How to animate image left to right in android

Manoj Pandey18728 28-Mar-2015

How to animate image left to right in android


Android provides powerful api to apply animation in out ui element drawing custom 2D and 3D animation. Animation makes our ui shining, quality feel and standard. Tween nimation calculates the animation with information such as the start point, end point, size, rotation, and other common aspects of an animation.

Here I am creating a sample of animation which image is moving left to right and right to left.

1.      Create an android project and api level must be greater than 10.

2.      Add view in activity_main.xml file

 

<?xmlversion="1.0"encoding="utf-8"?>

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/myLayout"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

 

    <ImageView

        android:id="@+id/imgLogo"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:src="@drawable/ic_launcher"/>

 

    <Button

        android:id="@+id/btnStart"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentBottom="true"

        android:layout_centerHorizontal="true"

        android:layout_marginBottom="20dp"

        android:layout_marginTop="30dp"

        android:text="Start Animation"/>

 

</RelativeLayout>

 

3.      Now add new  folder in res name as anim (res/anim) and create a my anim. xml file (Resource type must be Tweet Animation ) .

How to animate image left to right in android

 

4.      Add following xml code in res/anim/myanim.xml file

 

<?xmlversion="1.0"encoding="utf-8"?>

<setxmlns:android="http://schemas.android.com/apk/res/android"

    android:fillAfter="true"

    android:interpolator="@android:anim/linear_interpolator">

 

    <!-- Use startOffset to give delay between animations -->

 

 

    <!-- Move -->

    <translate

        android:duration="2000"

        android:fillAfter="true"

        android:startOffset="300"

        android:toXDelta="130%p"/>

    <translate

        android:duration="800"

        android:fillAfter="true"

        android:startOffset="2000"

        android:toYDelta="10%p"/>

    <translate

        android:duration="2000"

        android:fillAfter="true"

        android:startOffset="2800"

        android:toXDelta="-130%p"/>

 

</set>

 

5.       Now follow the below code in Mainactivity.class

 

package com.example.demoanimation;

 

import android.os.Bundle;

import android.app.Activity;

import android.view.View;

import android.view.animation.Animation;

import android.view.animation.AnimationUtils;

import android.view.animation.Animation.AnimationListener;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.RelativeLayout;

 

publicclass MainActivity extends Activity {

     ImageView imgLogo;

     Button btnStart;

     intpdfrom = 40;

     RelativeLayout layout;

     Animation animSequential;

 

     @Override

     protectedvoid onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

           layout = (RelativeLayout) findViewById(R.id.myLayout);

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

           btnStart = (Button) findViewById(R.id.btnStart);

           // repeat animation (left to right, right to left )

           // load the animation

animSequential = AnimationUtils.loadAnimation(getApplicationContext(),

                     R.anim.sequential);

 

           animSequential.setFillBefore(true);

           animSequential.setFillAfter(true);

 

           animSequential.setRepeatCount(Animation.INFINITE);

           animSequential.setRepeatMode(Animation.INFINITE);

           // button click event

           btnStart.setOnClickListener(new View.OnClickListener() {

                @Override

                publicvoid onClick(View v) {

                     // start the animation

                     pdfrom = 40;

                     imgLogo.startAnimation(animSequential);

 

                }

           });

           animSequential.setAnimationListener(new AnimationListener() {

 

                @Override

                publicvoid onAnimationStart(Animation animation) {

                     // TODO Auto-generated method stub

                     layout.setPadding(-50, 0, 0, 0);

 

                }

 

                @Override

                publicvoid onAnimationRepeat(Animation animation) {

                     // TODO Auto-generated method stub

                }

 

                @Override

                publicvoid onAnimationEnd(Animation animation) {

                     // TODO Auto-generated method stub

                     if (pdfrom < 241) {

                           imgLogo.setPadding(0, pdfrom, 0, 0);

                           pdfrom = pdfrom + 40;

                          imgLogo.startAnimation(animSequential);

                     }

else

{

 

animSequential.reset();

                               layout.setPadding(0, 0, 0, 0);

}

 

                }

           });

 

     }

 

}

 

Now run your application

How to animate image left to right in android   How to animate image left to right in android


Updated 07-Feb-2020

Leave Comment

Comments

Liked By