程序功能:
程序中有四个按钮(Alpha, Scale, Rotate, Translate)和一个图片:
分别点击四个按钮,则图片和对应的按钮分别执行:淡入淡出、缩放、旋转、移动动画效果。
代码如下:
AppMain.java
package lxy.litsoft; 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 AppMain extends Activity { //声明对象 private ImageView imageView; private Button btAlpha; private Button btScale; private Button btRotate; private Button btTranslate; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //实例化对象 imageView = (ImageView)findViewById(R.id.imageView01); btAlpha = (Button)findViewById(R.id.button01); btAlpha.setOnClickListener(new ButtonListener()); btScale = (Button)findViewById(R.id.button02); btScale.setOnClickListener(new ButtonListener()); btRotate = (Button)findViewById(R.id.button03); btRotate.setOnClickListener(new ButtonListener()); btTranslate = (Button)findViewById(R.id.button04); btTranslate.setOnClickListener(new ButtonListener()); } class ButtonListener implements OnClickListener{ public void onClick(View v) { if(v.getId() == R.id.button01){ //Alpha:淡入淡出效果 //创建AnimationSet对象 AnimationSet animationSet = new AnimationSet(true); //从透明度1,到透明度0 AlphaAnimation alphaAnimation = new AlphaAnimation(0,1); //动画执行时间 alphaAnimation.setDuration(1000); //把动画添加到动画集中 animationSet.addAnimation(alphaAnimation); //把动画绑定控件 imageView.startAnimation(animationSet); btAlpha.startAnimation(animationSet); } else if(v.getId() == R.id.button02){ //Scale:缩放效果 AnimationSet animationSet = new AnimationSet(true); ScaleAnimation scaleAnimation = new ScaleAnimation(1,1,0,1, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f); scaleAnimation.setDuration(1000); animationSet.addAnimation(scaleAnimation); imageView.startAnimation(animationSet); btScale.startAnimation(animationSet); } else if(v.getId() == R.id.button03){ //Rotate:旋转效果 AnimationSet animationSet = new AnimationSet(true); RotateAnimation rotateAnimation = new RotateAnimation(0f,360f,//旋转角度的变化范围 Animation.RELATIVE_TO_SELF, 0.5f, //旋转中心X的位置确定 Animation.RELATIVE_TO_SELF,0.5f); //旋转中心Y的位置确定 rotateAnimation.setDuration(1000); animationSet.addAnimation(rotateAnimation); // imageView.startAnimation(animationSet); btRotate.startAnimation(animationSet); }else if(v.getId() == R.id.button04){ //Translate:移动效果 AnimationSet animationSet = new AnimationSet(true); TranslateAnimation translateAnimation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_SELF,1, Animation.RELATIVE_TO_SELF,0); translateAnimation.setDuration(1000); animationSet.addAnimation(translateAnimation); imageView.startAnimation(animationSet); btTranslate.startAnimation(animationSet); } } } }main.xml
<?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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <LinearLayout android:orientation="vertical" android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/imageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="150dip" android:layout_marginLeft="200dip" android:src="@drawable/icon"></ImageView> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/button01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Alpha"></Button> <Button android:id="@+id/button02" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Scale"></Button> <Button android:id="@+id/button03" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Rotate"></Button> <Button android:id="@+id/button04" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Translate"></Button> </LinearLayout> </LinearLayout>
Tween Animations的通用属性
1.setDuration(long durationMills);//设置动画执行事件(单位:毫秒)
2.setFillAfter(boolean fillAfter);//如果fillAfter的值为true,则动画执行后,
控件将停留在执行结果的状态
3.setFillBefore(boolen fillBefore);//如果fillBefore的值为true,则动画执行后,
控件将回到动画执行之前的状态
4.setStartOffSet(long startOffSet);//设置动画执行之前的等待时间
5.setRepeatCount(int repeatCount);//设置动画重复执行的次数