Move view with animation effect in android from left to right

How to move view widgets to X-Direction of android activity surface like Graph view.

ObjectAnimator class gives us the methods to move views on activity surface by into x-direction also called as left to right side. We can define the moving range in dp like 100dp, 200dp etc. So here is the complete step by step tutorial for Move view with animation effect in android from left to right.

android-project-download-code-button

How to Move view with animation effect in android from left to right.

Code for MainActivity.java file.

 package com.moveviewwithanimationeffect_android_examples.com;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

 ObjectAnimator objectanimator;
 ImageView imgview;
 Button buttonView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 imgview = (ImageView)findViewById(R.id.imageView1);
 
 buttonView = (Button)findViewById(R.id.button1);
 
 objectanimator = ObjectAnimator.ofFloat(imgview,"x",300);
 
 buttonView.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 objectanimator.setDuration(4000);
 objectanimator.start();
 
 }
 });
 }
}

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.moveviewwithanimationeffect_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Click here to move view with animation" />

 <ImageView
 android:id="@+id/imageView1"
 android:layout_width="60dp"
 android:layout_height="60dp"
 android:src="@drawable/abc_list_focused_holo" />

</RelativeLayout>

Screenshots:

move-view

Move view with animation effect in android from left to right

Click here to download Move view with animation effect in android from left to right project file.

Leave a Reply

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