Android之Animations的使用

Animations一般分为两大类,一类是渐变的(Tweened):如淡入淡出,旋转,移动,缩放;另一类是Frame-by-Frame,就如电影一般由多张图片按照一定的时间间隔显示。

使用Tweened Animations的第一种使用步骤:

1. 创建一个AnimationSet对象,AnimationSet animationSet = new AnimationSet (boolean b);   //b为true表示共享Interpolator

2. 根据需要创建相应的Animation对象(AlphaAnimation、RotateAnimation、ScaleAnimation、TranslateAnimation)

3. 根据软件动画的需求,为Animation对象设置相应的数据

4. 将Animation对象添加到AnimationSet对象当中,使用addAnimation方法

5. 使用控件对象开始执行AnimationSet 

使用Tweened Animations的第二种使用步骤: 

1.  在res文件夹下面新建一个名为anim的文件夹

2. 创建xml文件,并首先加入set标签,改标签如下:

<set xmlns:android="http://schemas.android.com/apk/res/android"

android:interpolator="@android:anim/accelerate_interpolator">  //interpolator定义动画变化的速率

</set> 

3. 在该标签当中加入rotate,alpha,scale或者translate标签,例子如下:

<alpha

android:fromAlpha = "0.1"

android:toAlpha = "1.0"

android:duration = "3000"/> 

<rotate

android:fromDegrees="0"

android:toDegrees="+350"

android:pivotX="50%"

android:pivotY="50%"

android:duration="3000"/>

---注意---android:pivotX的值共有三种设置方法:

1. android:pivotX="50"这种方法使用绝对位置定位

2. android:pivotX="50%"这种方法相对于控件本身定位

3. android:pivotX="50%p"这种方法相对于控件的父控件定位 

4. 在代码当中使用AnimationUtils当中装载xml文件,并生成Animation对象,AnimationUtils.loadAnimation

Frame-By-Frame:使用方法

1. 在res/drawable当中创建一个xml文件,用于定义Animations的动画序列,以下是例子:

<animation-list 

xmlns:android="http://schemas.android.com/apk/res/android"

android:oneshot="false">

<item android:drawable="drawable/nv1"

android:duration="500"/> 

 <item android:drawable="drawable/nv2"

android:duration="500"/> 

<item android:drawable="drawable/nv3"
android:duration="500"/>  

<item android:drawable="drawable/nv4"

android:duration="500"/>  

</animation-list> 

2. imageView. setBackgroundResource("在res/drawable当中创建一个xml文件的Id");

3. AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();

4. animationDrawable.start(); 

原文地址:https://www.cnblogs.com/lee0oo0/p/2513008.html