main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout" android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/rotatetext" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Will be rotated." />
<Button android:id="@+id/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Rotate"
android:onClick="startAnimation"></Button>
<TextView android:id="@+id/scrolltext" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:lines="1"
android:text="Will be scrolled to the left."
android:scrollHorizontally="true" />
<Button android:id="@+id/Button02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Animate to left + bounce"
android:onClick="startAnimation"></Button>
<TextView android:id="@+id/fadeout" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:lines="1"
android:text="Will be faded out / in" android:scrollHorizontally="true" />
<Button android:id="@+id/Button03" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Fade"
android:onClick="startAnimation"></Button>
<Button android:id="@+id/Button04" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Add / Remove component"
android:onClick="startAnimation"></Button>
</LinearLayout>
the following file is in res.drawable/anim.xml
anim.xml
<?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="5000" android:pivotX="50%" android:pivotY="90%"
android:startOffset="0">
</rotate>
</set>
SampleAnimationActivity.Java
package com.animation;
import android.app.Activity;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.BounceInterpolator;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;
import android.widget.Toast;
public class SampleAnimation extends Activity implements AnimationListener {
private TextView animatedView3;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void startAnimation(View view) {
switch (view.getId()) {
case R.id.Button01:
// Show how to load an animation from XML
Animation animation1 = AnimationUtils.loadAnimation(this,
R.anim.anim);
animation1.setAnimationListener(this);
View animatedView1 = findViewById(R.id.rotatetext);
animatedView1.startAnimation(animation1);
break;
case R.id.Button02:
// Shows how to define a animation via code
// Also use an Interpolator (BounceInterpolator)
Paint paint = new Paint();
TextView animatedView2 = (TextView) findViewById(R.id.scrolltext);
float measureTextCenter = paint.measureText(animatedView2.getText()
.toString());
Animation animation2 = new TranslateAnimation(0f,
-measureTextCenter, 0.0f, 0.0f);
animation2.setDuration(5000);
animation2.setInterpolator(new BounceInterpolator());
animatedView2.startAnimation(animation2);
break;
case R.id.Button03:
// Demonstrate fading and adding an AnimationListener
animatedView3 = (TextView) findViewById(R.id.fadeout);
float from = 1.0f;
float to = 1.0f;
if (animatedView3.getVisibility() == View.INVISIBLE) {
from = to;
to = 1.0f;
}
Animation animation3 = new AlphaAnimation(from, to);
animation3.setDuration(5000);
animation3.setAnimationListener(this);
animatedView3.startAnimation(animation3);
break;
case R.id.Button04:
// Demonstrate LayoutAnimation
ViewGroup layout = (ViewGroup) findViewById(R.id.layout);
Animation animation4 = new AlphaAnimation(0.0f, 1.0f);
animation4.setDuration(5000);
LayoutAnimationController controller = new LayoutAnimationController(
animation4, 0);
layout.setLayoutAnimation(controller);
View button = findViewById(R.id.Button03);
if (button == null) {
layout.addView(button);
} else {
layout.removeView(button);
}
break;
default:
break;
}
}
public void onAnimationStart(Animation animation) {
Toast.makeText(this, "Animation started", Toast.LENGTH_SHORT).show();
}
public void onAnimationEnd(Animation animation) {
Toast.makeText(this, "Animation ended", Toast.LENGTH_SHORT).show();
if (animatedView3.getVisibility() == View.VISIBLE) {
animatedView3.setVisibility(View.INVISIBLE);
} else {
animatedView3.setVisibility(View.VISIBLE);
}
}
public void onAnimationRepeat(Animation animation) {
Toast.makeText(this, "Animation rep", Toast.LENGTH_SHORT).show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout" android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/rotatetext" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Will be rotated." />
<Button android:id="@+id/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Rotate"
android:onClick="startAnimation"></Button>
<TextView android:id="@+id/scrolltext" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:lines="1"
android:text="Will be scrolled to the left."
android:scrollHorizontally="true" />
<Button android:id="@+id/Button02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Animate to left + bounce"
android:onClick="startAnimation"></Button>
<TextView android:id="@+id/fadeout" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:lines="1"
android:text="Will be faded out / in" android:scrollHorizontally="true" />
<Button android:id="@+id/Button03" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Fade"
android:onClick="startAnimation"></Button>
<Button android:id="@+id/Button04" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Add / Remove component"
android:onClick="startAnimation"></Button>
</LinearLayout>
the following file is in res.drawable/anim.xml
anim.xml
<?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="5000" android:pivotX="50%" android:pivotY="90%"
android:startOffset="0">
</rotate>
</set>
SampleAnimationActivity.Java
package com.animation;
import android.app.Activity;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.BounceInterpolator;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;
import android.widget.Toast;
public class SampleAnimation extends Activity implements AnimationListener {
private TextView animatedView3;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void startAnimation(View view) {
switch (view.getId()) {
case R.id.Button01:
// Show how to load an animation from XML
Animation animation1 = AnimationUtils.loadAnimation(this,
R.anim.anim);
animation1.setAnimationListener(this);
View animatedView1 = findViewById(R.id.rotatetext);
animatedView1.startAnimation(animation1);
break;
case R.id.Button02:
// Shows how to define a animation via code
// Also use an Interpolator (BounceInterpolator)
Paint paint = new Paint();
TextView animatedView2 = (TextView) findViewById(R.id.scrolltext);
float measureTextCenter = paint.measureText(animatedView2.getText()
.toString());
Animation animation2 = new TranslateAnimation(0f,
-measureTextCenter, 0.0f, 0.0f);
animation2.setDuration(5000);
animation2.setInterpolator(new BounceInterpolator());
animatedView2.startAnimation(animation2);
break;
case R.id.Button03:
// Demonstrate fading and adding an AnimationListener
animatedView3 = (TextView) findViewById(R.id.fadeout);
float from = 1.0f;
float to = 1.0f;
if (animatedView3.getVisibility() == View.INVISIBLE) {
from = to;
to = 1.0f;
}
Animation animation3 = new AlphaAnimation(from, to);
animation3.setDuration(5000);
animation3.setAnimationListener(this);
animatedView3.startAnimation(animation3);
break;
case R.id.Button04:
// Demonstrate LayoutAnimation
ViewGroup layout = (ViewGroup) findViewById(R.id.layout);
Animation animation4 = new AlphaAnimation(0.0f, 1.0f);
animation4.setDuration(5000);
LayoutAnimationController controller = new LayoutAnimationController(
animation4, 0);
layout.setLayoutAnimation(controller);
View button = findViewById(R.id.Button03);
if (button == null) {
layout.addView(button);
} else {
layout.removeView(button);
}
break;
default:
break;
}
}
public void onAnimationStart(Animation animation) {
Toast.makeText(this, "Animation started", Toast.LENGTH_SHORT).show();
}
public void onAnimationEnd(Animation animation) {
Toast.makeText(this, "Animation ended", Toast.LENGTH_SHORT).show();
if (animatedView3.getVisibility() == View.VISIBLE) {
animatedView3.setVisibility(View.INVISIBLE);
} else {
animatedView3.setVisibility(View.VISIBLE);
}
}
public void onAnimationRepeat(Animation animation) {
Toast.makeText(this, "Animation rep", Toast.LENGTH_SHORT).show();
}
}