How to Perform Tween Animation(Zoom) in Android

In this tutorial how to perform Zoom In and Zoom Out Animation in Tween Animation in Android is shown.

Points to Remember:

  • For creating the zoom.xml file you will have to first create anim folder under res folder.
  • At the time of creating zoom.xml file you can either select ‘scale’ or ‘set’ as Root Element.
  • You can also perform zoom in and zoom out separately, for that you will have to create another file in anim folder, and don’t write the ‘android:startOffset’ property in this file else it will start the zoom out after some delay.

Code:

activity_main.xml

<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=”.MainActivity” >

<ImageView
android:id=”@+id/imageView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”134dp”
android:src=”@drawable/tweenanm” />

<Button
android:id=”@+id/b1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:layout_centerHorizontal=”true”
android:layout_marginBottom=”69dp”
android:onClick=”onZoom”
android:text=”Zoom” />

</RelativeLayout>

res/anim/zoom.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<set xmlns:android=”http://schemas.android.com/apk/res/android”>
<scale
android:fromXScale=”0.5″
android:toXScale=”3.0″
android:fromYScale=”0.5″
android:toYScale=”3.0″
android:duration=”5000″
android:pivotX=”50%”
android:pivotY=”50%”/>

<scale
android:fromXScale=”3.0″
android:toXScale=”0.5″
android:fromYScale=”3.0″
android:toYScale=”0.5″
android:duration=”5000″
android:pivotX=”50%”
android:pivotY=”50%”
android:startOffset=”5000″/>
</set>

 

MainActivity.java

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class MainActivity extends Activity{

ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView)findViewById(R.id.imageView1);
}

public void onZoom(View v)
{
Animation animation=AnimationUtils.loadAnimation(getApplicationContext(), R.anim.zoom);
iv.startAnimation(animation);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

https://youtu.be/fzMXcnRIX_M

Add a Comment

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