GTU Android Program 10 Tween Animation Example

GTU Android Program 10 Create an application that will have spinner with list of animation names. On selecting animation name , that animation should affect on the images displayed below.

GTU Android Program 10

We love Animation ! We play games which gives pleasure to us. Most of the android games are based on the Tween Animations.  Here I am explaining how you can do tween animation and animate one image as per the animation parameter you have setup. Here in the example I have prepared 4 different animation named Alpha, Rotate, Scale, Translate. Make sure you follow following step.


Step 1 : Create android project.

Step 2 : Add Image into res/drawable folder so that we can implement animation on it.

Step 3 : create folder “anim” in res folder.

Step 4 : add rotate.xml, scale.xml, alpha.xml and tranlate.xml into res/anim folder

For above steps refer following code.

If you are doing first time and confuse to do tween animation, I suggest download complete code using link at bottom.

Questions Related with Android Tween Animation.

  • Explain different types of Animation in Android.
  • Explain animation supported by Android taking suitable example
  • Differentiate Frame-by-frame Animation and Tweened Animation
  • What is Frame-by-Frame Animation? How it is different from Tweened Animation? Explain creating frame-by-frame animation taking suitable example.
  • Translate animation, taking 7 seconds to move an object up by 100 on y axis
    • use Translate animation with 7000 duration
  • Transparency change animation, taking 7 seconds to fade from fully transparent to fully opaque
    • use alpha animation with 7000 duration from 0 to 1
  • Rotation animation, taking 7 seconds to make one full clockwise rotation, pivoting from the center of the object.
    • use rotate animation with 7000 duration
  • Transparency change animation taking 10 seconds to fade in fully transparent to fully opaque
    • use android alpha animation, set duration to 10000,  from 0 to 1
  • Scaling animation, taking 5 seconds to double an object’s size, pivoting from the center of the object
    • use android scale animation, set duration to 5000
  • Translate  animation, taking 5 seconds to move an object up by 100 on the y axis and object should not jump back to its starting position when the animation finishes
    • use android translate animation.
  • For what purpose animation interpolator is used? How it can be specified
    programmatically?

    • An interpolator means rate of change of an animation (mostly it is used for smooth curve). This allows the basic animation effects (alpha, scale, translate, rotate)

Location : res\layout

FileName: 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"
    android:id="@+id/mainlayout"
    tools:context="gtu.android.prog10.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:text="Animation"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:entries="@array/animation"
        android:layout_marginTop="29dp" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/spinner1"
        android:layout_centerHorizontal="true"
        android:src="@drawable/image1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="48dp"
        android:text="Developed By: Adarsh Patel"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

 

Location : src\package\

FileName : MainActivity.java

package gtu.android.prog10;

/*
 *  GTU Android Program 10 Create an application that will have spinner with list of animation names. On selecting animation name , that animation should affect on the images displayed below.
 * 
 */


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

public class MainActivity extends Activity {

	Spinner sp1;
	ImageView iv1;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
	
		iv1 =(ImageView) findViewById(R.id.imageView1);
		sp1 = (Spinner) findViewById(R.id.spinner1);
		
		
		//================================================================
		//	Let's Animate Image based on the selection from Spinner
		// Don't forget to add all animation files into res/anim folder before you use following code.
		//===============================================================
		sp1.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				
				//========================
				//Alpha Animation
				//========================
				if(arg2==0)
				{
					Animation an = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha);
					iv1.startAnimation(an);
					
				}
				else if(arg2==1)
				{
					//========================
					//Rotate Animation
					//========================
					
					Animation an = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate);
					iv1.startAnimation(an);
				}
				else if(arg2==2)
				{
					//========================
					//Translate Animation
					//========================
					
					Animation an = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.translate);
					iv1.startAnimation(an);
				}
				else
				{
					//========================
					//scale Animation
					//========================
					
					Animation an = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scale);
					iv1.startAnimation(an);
				}
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});

	}

	@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;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

 

Location : res\anim

FileName: alpha.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0.0"
    android:duration="10000"
    android:toAlpha="1.0">
</alpha>

 

Location : res\anim

FileName: rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate 
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatMode="reverse"
    android:repeatCount="infinite"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:duration="3000"/>
    
</set>

 

Location : res\anim

FileName: scale.xml

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

 

Location : res\anim

FileName: translate.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
     android:fromXDelta="0"
     android:fromYDelta="0"
     android:toXDelta="50"
     android:toYDelta="50"
     android:duration="5000">
    

</translate>

 

Output of Alpha Animation Step 1

GTU Android Program 10

Output of Alpha Animation Step 2

GTU Android Program 10

Output of Rotate Animation

GTU Android Program 10

Output of Scale Animation

GTU Android Program 10

Output  of Translate Animation

GTU Android Program 10

Download Complete Project with all animation files and image

[sociallocker]Download [/sociallocker]

Download Few Popular Android Apps

  1. Bank IFSC Code : Now easily get bank ifsc code which you need while online transfer.
  2. All India PIN Code : Get all india PIN Code Numbers on 1 Android App.
  3. RTO Code : Get all India RTO Code on 1 Android App.
  4. Google 2 Step Code Speak : Now app will speak your google 2 step code, so no need to check mobile, code will reach you.
  5. All Gujarat ST Bus Stand Helpline Numbers (GSRTC Helpline Numbers)
  6. Mumbai City Bus Numbers (MSRTC Helpline Numbers)

  7. Andhra Pradesh City Bus Helpline Numbers (APSRTC Helpline Numbers)

  8. Bhavnagar Diary (Bhavnagar Business Directory)

  9. Baroda Diary (Baroda Business Directory)
  10. Surat Diary (Surat Business Directory)
  11. Ahmedabad Diary (Ahmedabad Business Directory)
  12. Rajkot Diary (Rajkot Business Directory)

1 thought on “GTU Android Program 10 Tween Animation Example

  1. Pingback: android frame by frame animation example

Comments are closed.