android中的四种基本动画




一、Tween动画

Tween动画通过对View的内容完成一系列的图像变化来实现效果(包括平移,缩放,旋转,改变透明度)。主要包括以下4种动画效果:

Android的animation由四种类型组成

在XML文件中:(以下的例子均来自网络,归原作者所有

  • </set>  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <alpha />  
  4.     <scale />  
  5.     <translate />  
  6.     <rotate />  
  7. </set>  
* alpha 渐变透明度动画效果
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <alpha    
  4.         android:fromAlpha="0.2"    
  5.         android:toAlpha="1.0"  
  6.         android:duration="3000" />  
  7.     <!-- 透明度控制动画效果 alpha 浮点型值:fromAlpha 属性为动画起始时透明度 toAlpha 属性为动画结束时透明度 说明:0.0表示完全透明 1.0表示完全不透明 以上值取0.0-1.0之间的float数据类型的数字 长整型值: duration 属性为动画持续时间 说明: 时间以毫秒为单位 -->  
* scale 渐变尺寸伸缩动画效果
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  4.         android:fromXScale="0.0"    
  5.         android:toXScale="1.4"    
  6.         android:fromYScale="0.0"  
  7.         android:toYScale="1.4"    
  8.         android:pivotX="50%"    
  9.         android:pivotY="50%"  
  10.         android:fillAfter="true"    
  11.         android:duration="700" />  
  12.     <!-- 尺寸伸缩动画效果 scale 属性:interpolator 指定一个动画的插入器 在我试验过程中,使用android.res.anim中的资源时候发现有三种动画插入器 accelerate_decelerate_interpolator 加速-减速 动画插入器 accelerate_interpolator 加速-动画插入器 decelerate_interpolator 减速- 动画插入器 其他的属于特定的动画效果 浮点型值: fromXScale 属性为动画起始时 X坐标上的伸缩尺寸 toXScale 属性为动画结束时 X坐标上的伸缩尺寸 fromYScale 属性为动画起始时Y坐标上的伸缩尺寸 toYScale 属性为动画结束时Y坐标上的伸缩尺寸 说明: 以上四种属性值 0.0表示收缩到没有 1.0表示正常无伸缩 值小于1.0表示收缩 值大于1.0表示放大 pivotX 属性为动画相对于物件的X坐标的开始位置 pivotY 属性为动画相对于物件的Y坐标的开始位置 说明: 以上两个属性值 从0%-100%中取值 50%为物件的X或Y方向坐标上的中点位置 长整型值: duration 属性为动画持续时间 说明: 时间以毫秒为单位 布尔型值: fillAfter 属性 当设置为true ,该动画转化在动画结束后被应用 -->  
  13. </set>  
* translate 画面转换位置移动动画效果
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <translate    
  4.         android:fromXDelta="30"    
  5.         android:toXDelta="-80"  
  6.         android:fromYDelta="30"    
  7.         android:toYDelta="300"    
  8.         android:duration="2000" />  
  9.     <!-- translate 位置转移动画效果 整型值: fromXDelta 属性为动画起始时 X坐标上的位置 toXDelta 属性为动画结束时X坐标上的位置 fromYDelta 属性为动画起始时 Y坐标上的位置 toYDelta 属性为动画结束时 Y坐标上的位置 注意: 没有指定fromXType toXType fromYType toYType 时候,默认是以自己为相对参照物 长整型值: duration 属性为动画持续时间 说明: 时间以毫秒为单位 -->  
  10. </set>  
* rotate 画面转移旋转动画效果
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <rotate android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  4.         android:fromDegrees="0"    
  5.         android:toDegrees="+360"    
  6.         android:pivotX="50%"  
  7.         android:pivotY="50%"    
  8.         android:duration="3000" />  
  9.     <!-- rotate 旋转动画效果 属性:interpolator 指定一个动画的插入器 在我试验过程中,使用android.res.anim中的资源时候发现有三种动画插入器 accelerate_decelerate_interpolator 加速-减速 动画插入器 accelerate_interpolator 加速-动画插入器 decelerate_interpolator 减速- 动画插入器 其他的属于特定的动画效果 浮点数型值: fromDegrees 属性为动画起始时物件的角度 toDegrees 属性为动画结束时物件旋转的角度 可以大于360度 说明: 当角度为负数——表示逆时针旋转 当角度为正数——表示顺时针旋转 (负数from——to正数:顺时针旋转) (负数from——to负数:逆时针旋转) (正数from——to正数:顺时针旋转) (正数from——to负数:逆时针旋转) pivotX 属性为动画相对于物件的X坐标的开始位置 pivotY 属性为动画相对于物件的Y坐标的开始位置 说明: 以上两个属性值 从0%-100%中取值 50%为物件的X或Y方向坐标上的中点位置 长整型值: duration 属性为动画持续时间 说明: 时间以毫秒为单位 -->  
  10. </set>  

在Java 源码中定义了相应的类,可以使用这些类的方法来获取和操作相应的属性:

* AlphaAnimation 渐变透明度动画效果
* ScaleAnimation  渐变尺寸伸缩动画效果
* TranslateAnimation 画面转换位置移动动画效果
* RotateAnimation  画面转移旋转动画效果

  使用代码实现动画:

public LinearLayout getLinearLayout(int key) {
  LinearLayout layout = new LinearLayout(this);
  // 实例化一个AnimationSet对象,该对象里面可以包含多个Animation的动画效果
  AnimationSet animationSet = new AnimationSet(true);
  // 这两个设置时关于动画执行完了之后,是还原前状态,还是停留在执行后的状态
  animationSet.setFillAfter(false);
  animationSet.setFillBefore(true);
  switch (key) {
   case 1:// 渐变透明度动画效果
    AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
    animationSet.addAnimation(alphaAnimation);
    layout.setBackgroundColor(Color.BLUE);
    break;
   case 2:// 渐变尺寸伸缩动画效果
    ScaleAnimation scaleAnimation = new ScaleAnimation(0f, 1f, 0f, 1f,
      Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    animationSet.addAnimation(scaleAnimation);
    layout.setBackgroundColor(Color.CYAN);
    break;
   case 3:// 画面转换位置移动动画效果
    TranslateAnimation translateAnimation = new TranslateAnimation(screenWidth, 0,
      screenHeight, 0);
    animationSet.addAnimation(translateAnimation);
    layout.setBackgroundColor(Color.DKGRAY);
    break;
   case 4:// 画面转移旋转动画效果
    RotateAnimation rotateAnimation = new RotateAnimation(0f, +360f,
      Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    animationSet.addAnimation(rotateAnimation);
    layout.setBackgroundColor(Color.GRAY);
    break;
   case 5:// 渐变尺寸伸缩和画面转移旋转动画效果
    ScaleAnimation scaleAnimation1 = new ScaleAnimation(0f, 1f, 0f, 1f,
      Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    RotateAnimation rotateAnimation1 = new RotateAnimation(0f, +360f,
      Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    animationSet.addAnimation(rotateAnimation1);
    animationSet.addAnimation(scaleAnimation1);
    layout.setBackgroundColor(Color.GREEN);
    break;
   case 6:// 渐变透明度和画面转移旋转动画效果(左右抽出)
    AlphaAnimation alphaAnimation1 = new AlphaAnimation(0.1f, 1.0f);
    TranslateAnimation translateAnimation1 = new TranslateAnimation(screenWidth, 0, 0,
      0);
    animationSet.addAnimation(translateAnimation1);
    animationSet.addAnimation(alphaAnimation1);
    layout.setBackgroundColor(Color.LTGRAY);
    break;
   case 7:// 渐变透明度和画面转移旋转动画效果(上下抽出)
    AlphaAnimation alphaAnimation2 = new AlphaAnimation(0.1f, 1.0f);
    TranslateAnimation translateAnimation3 = new TranslateAnimation(0, 0, screenHeight,
      0);
    animationSet.addAnimation(translateAnimation3);
    animationSet.addAnimation(alphaAnimation2);
    layout.setBackgroundColor(Color.RED);
    break;
   case 8:// 画面转换位置移动和画面转移旋转动画效果
    TranslateAnimation translateAnimation2 = new TranslateAnimation(screenWidth, 0,
      screenHeight, 0);
    RotateAnimation rotateAnimation2 = new RotateAnimation(0f, +360f,
      Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    animationSet.addAnimation(rotateAnimation2);
    animationSet.addAnimation(translateAnimation2);
    layout.setBackgroundColor(Color.MAGENTA);
    break;
   case 9:// 渐变透明度和渐变尺寸伸缩动画效果
    ScaleAnimation scaleAnimation2 = new ScaleAnimation(0.95f, 1f, 0.95f, 1f,
      Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    AlphaAnimation alphaAnimation3 = new AlphaAnimation(0.0f, 1.0f);
    animationSet.addAnimation(alphaAnimation3);
    animationSet.addAnimation(scaleAnimation2);
    layout.setBackgroundColor(Color.WHITE);
    break;
   default:
    break;
  }
  animationSet.setDuration(500);
  layout.startAnimation(animationSet);
  layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
    LayoutParams.MATCH_PARENT));
  Button back = new Button(this);
  back.setId(0);
  back.setText("返回");
  back.setOnClickListener(listener);
  layout.addView(back, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
  return layout;
 }

一个tween动画将对视图对象中的内容进行一系列简单的转换(位置,大小,旋转,透明性)。如果你有一个文本视图对象,你可以移动它,旋转它,让它变大或让它变小,如果文字下面还有背景图像,背景图像也会随着文件进行转换。

使用XML来定义Tween Animation

动画的XML文件在工程中res/anim目录,这个文件必须包含一个根元素,可以使<alpha><scale> <translate> <rotate>插值元素或者是把上面的元素都放入<set>元素组中,默认情况下,所以的动画指令都是同时发生的,为了让他们按序列发生,需要设置一个特殊的属性startOffset。动画的指令定义了你想要发生什么样的转换,当他们发生了,应该执行多长时间,转换可以是连续的也可以使同时的。例如,你让文本内容从左边移动到右边,然后旋转180度,或者在移动的过程中同时旋转,没个转换需要设置一些特殊的参数(开始和结束的大小尺寸的大小变化,开始和结束的旋转角度等等,也可以设置些基本的参数(例如,开始时间与周期),如果让几个转换同时发生,可以给它们设置相同的开始时间,如果按序列的话,计算开始时间加上其周期。

二、Frame动画

顺序播放事先做好的图像,是一种画面转换动画。

Frame Animation可以在XML Resource定义(还是存放到res\anim文件夹下),也可以使用AnimationDrawable中的API定义。由于Tween Animation与Frame Animation有着很大的不同,因此XML定义的格式也完全不一样,其格式是:首先是animation-list根节点,animation-list根节点中包含多个item子节点,每个item节点定义一帧动画:当前帧的drawable资源和当前帧持续的时间。下面对节点的元素加以说明:

XML属性说明

drawable 当前帧引用的drawable资源
duration 当前帧显示的时间(毫秒为单位)
oneshot 如果为true,表示动画只播放一次停止在最后一帧上,如果设置为false表示动画循环播放。
visible 规定drawable的初始可见性,默认为flase;
原文地址:https://www.cnblogs.com/crazywenza/p/2785680.html