Android change view scale with animation example programmatically

How to minimize view width, height using animation with ObjectAnimator so view will decrease slowly smoothly.

ObjectAnimator holds all the simple animation functions and one of them is setting up scaleX and scaleY of any given view so after setting up animation timing it will smoothly minimize the whole view with our given width and heigh form. So here is the complete step by step tutorial for Android change view scale with animation example programmatically.

android-project-download-code-button

Android change view scale with animation example programmatically.

Code for MainActivity.java file.

 package com.changescaleanimation_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.TextView;

public class MainActivity extends Activity {

 TextView Textview;
 Button button;
 ObjectAnimator objectanimator1, objectanimator2;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 Textview = (TextView)findViewById(R.id.textView1);
 
 button = (Button)findViewById(R.id.button1);
 
 objectanimator1 = ObjectAnimator.ofFloat(Textview,"scaleX",0.6f);
 
 objectanimator2 = ObjectAnimator.ofFloat(Textview,"scaleY",0.6f);
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 objectanimator1.setDuration(4000);
 objectanimator2.setDuration(4000);
 
 objectanimator1.start();
 objectanimator2.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.changescaleanimation_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="fill_parent"
 android:layout_height="100dp"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="122dp"
 android:background="#04d877"
 android:gravity="center"
 android:text="TextView"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="25dp"
 android:text="Click here to change scale animation example programmatically" />

</RelativeLayout>

Screenshot :

animation scale

Android change view scale with animation example programmatically

Click here to download Android change view scale using animation example programmatically project.

Leave a Reply

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