---
title: "How to animate image left to right in android"  
description: "In this article I am explaining wow to animate image left to right in android"  
author: "Manoj Pandey"  
published: 2015-03-28  
updated: 2020-02-07  
canonical: https://www.mindstick.com/articles/1698/how-to-animate-image-left-to-right-in-android  
category: "android"  
tags: ["android", "animation"]  
reading_time: 3 minutes  

---

# How to animate image left to right in android

## How to animate image left to right in android

\

[Android](https://www.mindstick.com/articles/1567/navigation-drawer-implementation-in-android) provides powerful api to apply [animation](https://www.mindstick.com/articles/13040/seven-helpful-tips-to-start-and-grow-an-animation-video-company) in out ui element drawing [custom](https://www.mindstick.com/blog/134981/midwest-tropical-is-your-best-source-for-custom-water-features) 2D and 3D animation. Animation makes our ui shining, [quality](https://www.mindstick.com/articles/13022/7-easy-tips-for-creating-quality-content) feel and standard. **Tween nimation** calculates the animation with information such as the start point, end point, size, rotation, and other [common](https://yourviews.mindstick.com/story/1215/the-most-common-sexually-transmitted-infections) 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](https://yourviews.mindstick.com/story/1788/things-to-ensure-your-construction-project-is-successful) and api level [must](https://yourviews.mindstick.com/view/84137/10-must-know-image-optimization-tips) be greater than 10.

2. Add view in activity_main.[xml file](https://www.mindstick.com/articles/75/how-to-read-and-write-xml-file-through-c-sharp)

```
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns: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](https://www.mindstick.com/blog/43433/top-best-resources-for-women-about-beauty-and-health) type must be **Tweet Animation** ) .

![How to animate image left to right in android](https://www.mindstick.com/mindstickarticle/c377fb15-658e-4202-811c-5896b5f6c6d2/images/45f46c54-4df1-447d-9a37-8bf9cb45579c.png)

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

```
<?xml version="1.0" encoding="utf-8"?><set xmlns: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; public class MainActivity extends Activity {     ImageView imgLogo;     Button btnStart;     int pdfrom = 40;     RelativeLayout layout;     Animation animSequential;      @Override     protected void 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 animationanimSequential = 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                public void onClick(View v) {                     // start the animation                     pdfrom = 40;                     imgLogo.startAnimation(animSequential);                 }           });           animSequential.setAnimationListener(new AnimationListener()            {                 @Override                public void onAnimationStart(Animation animation) {                     // TODO Auto-generated method stub                     layout.setPadding(-50, 0, 0, 0);                 }                 @Override                public void onAnimationRepeat(Animation animation) {                     // TODO Auto-generated method stub                }                 @Override                public void 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](https://www.mindstick.com/mindstickarticle/c377fb15-658e-4202-811c-5896b5f6c6d2/images/cc708802-23ed-4b36-9722-ae148343ec26.png) ![How to animate image left to right in android](https://www.mindstick.com/mindstickarticle/c377fb15-658e-4202-811c-5896b5f6c6d2/images/ec170ea3-6682-4b6f-a107-288b7ec53f3d.png)

---

Original Source: https://www.mindstick.com/articles/1698/how-to-animate-image-left-to-right-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
