Android 动画使用

来源:互联网 发布:好用的精华液 知乎 编辑:程序博客网 时间:2024/06/01 09:14

一、动画类型

Androidanimation由四种类型组成:alpha、scale、translate、rotate

XML配置文件中

alpha
渐变透明度动画效果
scale
渐变尺寸伸缩动画效果
translate
画面转换位置移动动画效果
rotate
画面转移旋转动画效果

Java Code代码 

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

二、Android动画模式

Animation主要有两种动画模式:tweened  frame

  • 一种是tweened animation(渐变动画) 
XML
JavaCode
alpha
AlphaAnimation
scale
ScaleAnimation

  • 一种是frame by frame(画面转换动画) 
XML
JavaCode
translate
TranslateAnimation
rotate
RotateAnimation

三、XML文件中定义动画

① 打开Eclipse,新建Android工程

② res目录中新建anim文件夹

③ anim目录中新建一个myanim.xml(注意文件名小写)

④ 加入XML的动画代码

  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>

四、Android XML动画解析

1. 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.1"
  5. android:toAlpha="1.0"
  6. android:duration="3000"
  7. /> 
  8. <!-- 透明度控制动画效果 alpha
  9.         浮点型值:
  10.             fromAlpha 属性为动画起始时透明度
  11.             toAlpha   属性为动画结束时透明度
  12.             说明: 
  13.                 0.0表示完全透明
  14.                 1.0表示完全不透明
  15.             以上值取0.0-1.0之间的float数据类型的数字

  16.         长整型值:
  17.             duration  属性为动画持续时间
  18.             说明:     
  19.                 时间以毫秒为单位
  20. -->
  21. </set>
2. Scale
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">
  3.    <scale  
  4.           android:interpolator=
  5.                      "@android:anim/accelerate_decelerate_interpolator"
  6.           android:fromXScale="0.0"
  7.           android:toXScale="1.4"
  8.           android:fromYScale="0.0"
  9.           android:toYScale="1.4"
  10.           android:pivotX="50%"
  11.           android:pivotY="50%"
  12.           android:fillAfter="false"
  13.           android:duration="700" />
    1. </set>
    2. <!-- 尺寸伸缩动画效果 scale
    3.        属性:interpolator 指定一个动画的插入器
    4.         在我试验过程中,使用android.res.anim中的资源时候发现
    5.         有三种动画插入器:
    6.             accelerate_decelerate_interpolator  加速-减速 动画插入器
    7.             accelerate_interpolator        加速-动画插入器
    8.             decelerate_interpolator        减速- 动画插入器
    9.         其他的属于特定的动画效果
    10.       浮点型值:
    11.          
    12.             fromXScale 属性为动画起始时 X坐标上的伸缩尺寸    
    13.             toXScale   属性为动画结束时 X坐标上的伸缩尺寸     
    14.         
    15.             fromYScale 属性为动画起始时Y坐标上的伸缩尺寸    
    16.             toYScale   属性为动画结束时Y坐标上的伸缩尺寸    
    17.         
    18.             说明:
    19.                  以上四种属性值    
    20.     
    21.                     0.0表示收缩到没有 
    22.                     1.0表示正常无伸缩     
    23.                     值小于1.0表示收缩  
    24.                     值大于1.0表示放大
      1.  pivotX     属性为动画相对于物件的X坐标的开始位置
      2.             pivotY     属性为动画相对于物件的Y坐标的开始位置
      3.         
      4.             说明:
      5.                     以上两个属性值 从0%-100%中取值
      6.                     50%为物件的X或Y方向坐标上的中点位置
      7.         
      8.         长整型值:
      9.             duration  属性为动画持续时间
      10.             说明:   时间以毫秒为单位

      11.         布尔型值:
      12.             fillAfter 属性 当设置为true ,该动画转化在动画结束后被应用
      13. -->
      3. 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. />
      10. <!-- translate 位置转移动画效果
      11.         整型值:
      12.             fromXDelta 属性为动画起始时 X坐标上的位置    
      13.             toXDelta   属性为动画结束时 X坐标上的位置
      14.             fromYDelta 属性为动画起始时 Y坐标上的位置
      15.             toYDelta   属性为动画结束时 Y坐标上的位置
      16.             注意:
      17.                      没有指定fromXType toXType fromYType toYType 时候,
      18.                      默认是以自己为相对参照物             
      19.         长整型值:
      20.             duration  属性为动画持续时间
      21.             说明:   时间以毫秒为单位
      22. -->
      23. </set>
      4. Rotate

      1. <?xml version="1.0" encoding="utf-8"?>
      2. <set xmlns:android="http://schemas.android.com/apk/res/android">
      3. <rotate 
      4.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"
      5.         android:fromDegrees="0" 
      6.         android:toDegrees="+350"         
      7.         android:pivotX="50%" 
      8.         android:pivotY="50%"     
      9.         android:duration="3000" />  
      10. <!-- rotate 旋转动画效果
      11.        属性:interpolator 指定一个动画的插入器
      12.              在我试验过程中,使用android.res.anim中的资源时候发现
      13.              有三种动画插入器:
      14.                 accelerate_decelerate_interpolator   加速-减速 动画插入器
      15.                 accelerate_interpolator               加速-动画插入器
      16.                 decelerate_interpolator               减速- 动画插入器
      17.              其他的属于特定的动画效果

      18.        浮点数型值:
      19.             fromDegrees 属性为动画起始时物件的角度    
      20.             toDegrees   属性为动画结束时物件旋转的角度 可以大于360度   


      21.             说明:
      22.                      当角度为负数——表示逆时针旋转
      23.                      当角度为正数——表示顺时针旋转              
      24.                      (负数from——to正数:顺时针旋转)   
      25.                      (负数from——to负数:逆时针旋转) 
      26.                      (正数from——to正数:顺时针旋转) 
      27.                      (正数from——to负数:逆时针旋转)       
      1.  pivotX     属性为动画相对于物件的X坐标的开始位置
      2.             pivotY     属性为动画相对于物件的Y坐标的开始位置

      3.             说明:        以上两个属性值 从0%-100%中取值
      4.                          50%为物件的X或Y方向坐标上的中点位置

      5.         长整型值:
      6.             duration  属性为动画持续时间
      7.             说明:       时间以毫秒为单位
      8. -->
      9. </set>
      XML中使用动画效果
      1. public static Animation loadAnimation (Context context, int id) 
      2. //第一个参数Context为程序的上下文    
      3. //第二个参数id为动画XML文件的引用
      4. //例子:
      5. myAnimation= AnimationUtils.loadAnimation(this, R.anim.my_action);
      6. //使用AnimationUtils类的静态方法loadAnimation()来加载XML中的动画XML文件
      1. //在代码中定义 动画实例对象
      2. private Animation myAnimation_Alpha;
      3. private Animation myAnimation_Scale;
      4. private Animation myAnimation_Translate;
      5. private Animation myAnimation_Rotate;

      6.     //根据各自的构造方法来初始化一个实例对象
      7. myAnimation_Alpha = new AlphaAnimation(0.1f, 1.0f);

      8. myAnimation_Scale = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
      9.              Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

      10. myAnimation_Translate = new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f);

      11. myAnimation_Rotate = new RotateAnimation(0.0f, +350.0f,
      12.                Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);

      六、Android 代码动画解析

      1. AlphaAnimation

      AlphaAnimation类对象定义
      1.    1. private AlphaAnimation myAnimation_Alpha;
      AlphaAnimation类对象构造
      1. AlphaAnimation(float fromAlpha, float toAlpha) 
      2. //第一个参数fromAlpha为 动画开始时候透明度
      3. //第二个参数toAlpha为 动画结束时候透明度
      4. myAnimation_Alpha = new AlphaAnimation(0.1f, 1.0f);
      5. //说明: 
      6. //                0.0表示完全透明
      7. //                1.0表示完全不透明
      设置动画持续时间
      1. myAnimation_Alpha.setDuration(5000);
      2. //设置时间持续时间为 5000毫秒

      2. ScaleAnimation
      ScaleAnimation类对象定义
      1. private ScaleAnimation myAnimation_Scale;
      ScaleAnimation类对象构造

        1. ScaleAnimation(float fromX, float toX, float fromY, float toY,
        2.            int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) 
        3. //第一个参数fromX为动画起始时 X坐标上的伸缩尺寸    
        4. //第二个参数toX为动画结束时 X坐标上的伸缩尺寸     
        5. //第三个参数fromY为动画起始时Y坐标上的伸缩尺寸    
        6. //第四个参数toY为动画结束时Y坐标上的伸缩尺寸  
        7. /*说明:
        8.                     以上四种属性值    
        9.                     0.0表示收缩到没有 
        10.                     1.0表示正常无伸缩     
        11.                     值小于1.0表示收缩  
        12.                     值大于1.0表示放大
        13. */
        14. //第五个参数pivotXType为动画在X轴相对于物件位置类型  
        15. //第六个参数pivotXValue为动画相对于物件的X坐标的开始位置
        16. //第七个参数pivotXType为动画在Y轴相对于物件位置类型   
        17. //第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置
        18. myAnimation_Scale = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
        19.              Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        设置动画持续时间
        1. myAnimation_Scale.setDuration(700);
        2. //设置时间持续时间为 700毫秒

        3. TranslateAnimation

        ranslateAnimation类对象定义

        1. private TranslateAnimation myAnimation_Translate;
        TranslateAnimation类对象构造
        1. TranslateAnimation(float fromXDelta, float toXDelta,
        2.                        float fromYDelta, float toYDelta) 
        3. //第一个参数fromXDelta为动画起始时 X坐标上的移动位置    
        4. //第二个参数toXDelta为动画结束时 X坐标上的移动位置      
        5. //第三个参数fromYDelta为动画起始时Y坐标上的移动位置     
        6. //第四个参数toYDelta为动画结束时Y坐标上的移动位置
        设置动画持续时间
        1. myAnimation_Translate = new TranslateAnimation(10f, 100f, 10f, 100f);
        2. myAnimation_Translate.setDuration(2000);
        3. //设置时间持续时间为 2000毫秒

        4. RotateAnimation
        RotateAnimation类对象定义
        1. private RotateAnimation myAnimation_Rotate;

      1. RotateAnimation类对象构造
        1. RotateAnimation(float fromDegrees, float toDegrees, 
        2.             int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
        3. //第一个参数fromDegrees为动画起始时的旋转角度    
        4. //第二个参数toDegrees为动画旋转到的角度   
        5. //第三个参数pivotXType为动画在X轴相对于物件位置类型  
        6. //第四个参数pivotXValue为动画相对于物件的X坐标的开始位置
        7. //第五个参数pivotXType为动画在Y轴相对于物件位置类型   
        8. //第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置
        9. myAnimation_Rotate = new RotateAnimation(0.0f, +350.0f,
        10.                Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
        设置动画持续时间
        1. myAnimation_Rotate.setDuration(3000);
        2. //设置时间持续时间为 3000毫秒

        如何Java代码中使用动画效果
        使用从View父类继承过来的方法startAnimation()来为View或是子类View等等添加一个动画效果
        1. public void startAnimation (Animation animation)
        2. view.startAnimation(myAnimation_Alpha);
        3. view.startAnimation(myAnimation_Scale);
        4. view.startAnimation(myAnimation_Translate);
        5. view.startAnimation(myAnimation_Rotate);



    0 0