Friday, March 23, 2012

Simple View Animation in ANDROID?


Simple View Animation in ANDROID?


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 javacode into it.
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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.
?
1
2
3
4
5
<!--?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
?
01
02
03
04
05
06
07
08
09
10
11
12
<!--?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">
 
</textview></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

No comments: