Android:Animation的简单学习


Animation动画效果。提供了一系列的动画效果,可以应用大多数 的控件。

一、Animations从总体上来说可以分为两大类:

1、TweenedAnimations:该类提供了旋转,移动,伸展,和淡出竺效果;

2、Frame-by-FrameAmimations:这一类可以创建一个Drawable序列:这些Drawable可以按照指定的时间间歇一个一个的显示。

二、TwenedAnimations的分类

a)        Alpha:淡入淡出效果

b)        Scale:缩放效果

c)        Rotate:旋转效果

d)        Translate:移动效果

这四种动画效果对应四个不同的类,都有不同的参数,但是这四个不同的动画效果有共同的参数:下面简单介绍下面几种:

setDuration(long durationMills)设置动画持续时间,单位毫秒 ;

setFillAfter(boolean fillAfter)如果为true的话,动画执行后,控件停留在执行结束的状态;

setFillBefore(boolean fillBefore)

setStartOffSet(long startOffSet)设置动画执行之前等待时间;

setRepeatCount(int repeatCount)设置动画重复的次数

下面简单介绍一下,TewnedAnimation的使用步骤:

1、创建一个AnimationSet,存放动画集合,也可以只有一个动画。

2、根据需要创建对应的Animation

3、根据动画的需求,为Animation创建相对应的数据。

4、将Animation对象添加到AnimationSet

5、使用控件开始执行Animation:textView.startAnimation(AnimateionSet)当然里面的参数也可以是Animate,由于Animation是AnimationSet的父类,所以不会出错

我们来做一个简单的例子,了解Animation的用法 :

1、 在main.xml文件里面添加四个按钮控件,和一个图片控件,用来进行测试,图片用于显示动画效果;

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <ImageView android:id="@+id/image"  
  8.     android:src="@drawable/icon"  
  9.     android:layout_width="wrap_content"  
  10.     android:layout_height="wrap_content"  
  11.     android:layout_margin="100dip"/>  
  12. <Button android:id="@+id/alpha"  
  13.     android:layout_width="fill_parent"  
  14.     android:layout_height="wrap_content"  
  15.     android:text="@string/alpha"/>  
  16. <Button android:id="@+id/scale"  
  17.     android:layout_width="fill_parent"  
  18.     android:layout_height="wrap_content"  
  19.     android:text="@string/scale"/>  
  20. <Button android:id="@+id/rotate"  
  21.     android:layout_width="fill_parent"  
  22.     android:layout_height="wrap_content"  
  23.     android:text="@string/rotate"/>  
  24. <Button android:id="@+id/translate"  
  25.     android:layout_width="fill_parent"  
  26.     android:layout_height="wrap_content"  
  27.     android:text="@string/translate"/>  
  28. </LinearLayout>  
2、编写Activity类,添加监听:并且在对应的监听事件上 加上动画效果(按上面介绍了的步骤进行)
  1. public class HelloAnimationActivity extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.     Button b_alpha;  
  4.     Button b_scale;  
  5.     Button b_rotate;  
  6.     Button b_translate;  
  7.     ImageView iv;  
  8.     AnimationSet animationSet;  
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.main);  
  13.          
  14.         iv = (ImageView)findViewById(R.id.image);  
  15.         b_alpha = (Button)findViewById(R.id.alpha);  
  16.         b_scale = (Button)findViewById(R.id.scale);  
  17.         b_rotate = (Button)findViewById(R.id.rotate);  
  18.         b_translate = (Button)findViewById(R.id.translate);  
  19.           
  20.         b_alpha.setOnClickListener(new AlpahListener());  
  21.         b_scale.setOnClickListener(new ScaleListener());  
  22.         b_rotate.setOnClickListener(new RotateListener());  
  23.         b_translate.setOnClickListener(new TranslateListener());  
  24.           
  25.     }  
  26.       
  27.     class AlpahListener implements OnClickListener{  
  28.           
  29.         @Override  
  30.         public void onClick(View v) {  
  31.             //创建一个AnimationSet对象   
  32.             AnimationSet animationSet = new AnimationSet(true);  
  33.             //创建一个AlphaAnimation对象   
  34.             AlphaAnimation alphaAnimation = new AlphaAnimation(10);  
  35.             //设置动画执行的时间(单位:毫秒)   
  36.             alphaAnimation.setDuration(1000);  
  37.             //将AlphaAnimation对象添加到AnimationSet当中   
  38.             animationSet.addAnimation(alphaAnimation);  
  39.             //使用ImageView的startAnimation方法开始执行动画   
  40.             iv.startAnimation(animationSet);  
  41.         }  
  42.           
  43.     }  
  44.       
  45.     class ScaleListener implements OnClickListener{  
  46.         /** 
  47.          * 从大到小的动画,x轴1到0.1,y轴1到0.1。中心为图片的中心(0.5f,0.5f) 
  48.          */  
  49.         @Override  
  50.         public void onClick(View arg0) {  
  51.             AnimationSet animationSet = new AnimationSet(true);  
  52.             ScaleAnimation scaleAnimation = new ScaleAnimation(10.1f, 10.1f,  
  53.                     Animation.RELATIVE_TO_SELF, 0.5f,  
  54.                     Animation.RELATIVE_TO_SELF, 0.5f);  
  55.             animationSet.addAnimation(scaleAnimation);  
  56.             animationSet.setStartOffset(1000);  
  57.             animationSet.setFillAfter(true);  
  58.             animationSet.setFillBefore(false);  
  59.             animationSet.setDuration(2000);  
  60.             iv.startAnimation(animationSet);  
  61.               
  62.         }  
  63.           
  64.     }  
  65.       
  66.     class RotateListener implements OnClickListener{  
  67.         /** 
  68.          * 以图片中心旋转360度;(0.5f,0.5f)表示图片的中心 
  69.          */  
  70.         @Override  
  71.         public void onClick(View arg0) {  
  72.             // TODO Auto-generated method stub   
  73.             animationSet = new AnimationSet(true);  
  74.             RotateAnimation rotateAnimation = new RotateAnimation(0360,  
  75.                     Animation.RELATIVE_TO_SELF, 0.5f,  
  76.                     Animation.RELATIVE_TO_SELF, 0.5f);  
  77.             rotateAnimation.setDuration(5000);  
  78.             animationSet.addAnimation(rotateAnimation);  
  79.             iv.setAnimation(rotateAnimation);  
  80.         }  
  81.           
  82.     }  
  83.       
  84.     class TranslateListener implements OnClickListener{  
  85.   
  86.         @Override  
  87.         public void onClick(View arg0) {  
  88.             // TODO Auto-generated method stub   
  89.             animationSet = new AnimationSet(true);  
  90.             TranslateAnimation translateAnimation = new TranslateAnimation(  
  91.                     Animation.RELATIVE_TO_SELF, 0f,  
  92.                     Animation.RELATIVE_TO_SELF, 0.5f,  
  93.                     Animation.RELATIVE_TO_SELF, 0f,  
  94.                     Animation.RELATIVE_TO_SELF, 1.0f);  
  95.             animationSet.addAnimation(translateAnimation);  
  96.             iv.setAnimation(animationSet);  
  97.         }  
  98.           
  99.     }  
  100. }  
  • 1
  • 2
  • 3
  • 下一页

相关内容