Activity:
package cn.howay;
import android.R.anim;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class AnimationTest extends Activity{
private ImageView imageView = null;
private Button alaphaButton = null;
private Button rotateButton = null;
private Button translateButton = null;
private Button scaleButton = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animationtest);
imageView = (ImageView) findViewById(R.id.image);
alaphaButton = (Button) findViewById(R.id.alpha);
rotateButton = (Button) findViewById(R.id.rotate);
translateButton = (Button) findViewById(R.id.translate);
scaleButton = (Button) findViewById(R.id.scale);
alaphaButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//创建一个AnimationSet对象
AnimationSet animationSet = new AnimationSet(true);
//创建一个AlaphaAnimation对象
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
//设置动画执行时间
alphaAnimation.setDuration(5000);
//将AlphaAnimation添加到AnimationSet中
animationSet.addAnimation(alphaAnimation);
//使用ImageView中的startAnimation()方法开始执行动画
imageView.startAnimation(animationSet);
}
});
rotateButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
RotateAnimation rotateAnimation = new RotateAnimation(
0, 360,
Animation.RELATIVE_TO_PARENT, 1f,
Animation.RELATIVE_TO_PARENT, 0f);
rotateAnimation.setDuration(5000);
animationSet.addAnimation(rotateAnimation);
imageView.startAnimation(animationSet);
}
});
translateButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 1.0f);
translateAnimation.setDuration(5000);
animationSet.addAnimation(translateAnimation);
imageView.startAnimation(translateAnimation);
}
});
scaleButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation scaleAnimation = new ScaleAnimation(
1, 0.1f, 1, 0.1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animationSet.addAnimation(scaleAnimation);
animationSet.setStartOffset(1000);
animationSet.setFillAfter(true);
animationSet.setFillAfter(false);
animationSet.setDuration(5000);
imageView.startAnimation(scaleAnimation);
}
});
}
}
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="200px"
>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"
/>
</LinearLayout>
<Button
android:id="@+id/alpha"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="测试Alpha"
/>
<Button
android:id="@+id/scale"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="测试Scale"
/>
<Button
android:id="@+id/translate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="测试Translate"
/>
<Button
android:id="@+id/rotate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="测试rotate"
/>
</LinearLayout>