Simple View Animation in ANDROID?

By | May 2, 2013

Hi all…..
In this post I will show you a simple animation using a textView.
It’s really simple.

Steps.

1. Create a fresh project and copy this java code into it.

package pack.coderzheaven;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.Toast;

public class AnimationDemo extends Activity implements AnimationListener {

	View v;
	Boolean STOP = false;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public void startAnimation(View view) {
		Animation animation = AnimationUtils.loadAnimation(this,R.anim.animation);
		animation.setAnimationListener(this);
		View animatedView = findViewById(R.id.textview);
		animatedView.startAnimation(animation);
	}

    public void stopAnimation(View view){
    	Toast.makeText(this, "Animation will stop after this loop.", Toast.LENGTH_SHORT).show();
    	STOP = true;
    }
	@Override
	public void onAnimationStart(Animation animation) {
		Toast.makeText(this, "Animation started", Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onAnimationEnd(Animation animation) {
		Toast.makeText(this, "Animation ended", Toast.LENGTH_SHORT).show();
		if(STOP == false)
			startAnimation(v);
	}

	@Override
	public void onAnimationRepeat(Animation animation) {
		Toast.makeText(this, "Animation rep", Toast.LENGTH_SHORT).show();
	}
}

2. Create a folder named “anim” inside the res folder and create a file named “animation.xml” and copy this code into it.

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
	android:shareInterpolator="true">
	<rotate android:fromDegrees="0" android:toDegrees="360"
		android:duration="10000" android:pivotX="50%" android:pivotY="50%"
		android:startOffset="10">
	</rotate>
</set>

3. copy this code to your “main.xml” file

<?xml version="1.0" encoding="utf-8"?>
<linearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

	<button
		android:id="@+id/Button01"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="Start Animation"
		android:onClick="startAnimation">
	</button>

	<button
		android:id="@+id/Button02"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="Stop Animation"
		android:onClick="stopAnimation">
	</button>

	<textView
		android:id="@+id/textview"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="@string/hello" />

</linearLayout>

4. Clean your project and run it.

Click the start animation button will start the animation and stop button will stop the animation after one loop.
The code is self explanatory, so I am not going to explain the code.

ANDROID Animation example

ANDROID Animation example

Join the Forum discussion on this post

5 thoughts on “Simple View Animation in ANDROID?

  1. Pingback: Mr. Android » Blog Archive » Simple View Animation in ANDROID?

  2. Darci Feth

    This domain appears to get a good ammount of visitors. How do you advertise it? It gives a nice individual spin on things. I guess having something authentic or substantial to give info on is the most important thing.

    Reply
    1. James Post author

      Yeah, we started this site about 100 days ago and now we have good traffic. We won’t advertise our site our publicity is due to the type the content we give to our visitors.

      Reply

Leave a Reply

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