Mars视频笔记——Animation

来源:互联网 发布:app网站制作软件 编辑:程序博客网 时间:2024/05/19 20:48

Animations的使用(1)

什么是Animations

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

 

Animations的分类

1 Tweened Animations 渐变动画

提供了旋转,移动,伸展,淡出等效果

2 Frame-by-Frame Animations

可以创建一个Drawable序列,按照指定时间间歇一个个显示

 

Tweened Animations

1 Alpha 淡入淡出效果

2 Scale 缩放效果

3 Rotate 旋转效果

4 Translate 移动效果

 

Animations的第一种使用方法代码实现,xml实现)

 

使用Tweened Animations的步骤

1 创建一个AnimationSet对象

AnimationSet animationSet=new AnimationSet(true); 

2 根据需要创建相应的Animation对象(旋转,移动,伸展,淡出)

AlphaAnimation alphaAnimation = new AlphaAnimation(1,0); //参数为from..to..

*其他:

RotateAnimation rotateAnimation = new RotateAnimation(0,360,Animation.RELATIVE_TO_PARENT(有3种),1f,Animation.RELATIVE_TO_PARENT,0f);

3种坐标种类Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_PARENT,Animation.ABSOLUTE

ScaleAnimation scaleAnimation = new ScaleAnimation(1,0.1f,1,0.1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);

TranslateAnimation .......

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

animationSet.setDuration(1000); //动画执行时间

4 将Animation对象添加到AnimationSet对象中

animationSet.addAnimation(alphaAnimation);

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

imageView.startAnimation(animationSet);

 

Tweened Animations 通用属性

setDuration

setFillAfter

SetFillBefore

setStartOffSet

setRepeatCount


Animations使用(2)

接上篇

Animations的第二种使用方法(第一种见1)

步骤

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

 

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

 

Xml代码 复制代码 收藏代码
  1. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:interpolator="@android:anim/accelerate_interpolator">  
  3.     ...   
  4. </set>  
 

3 在该标签中加入rotate,alpha,scale或者translate标签

例:

Alpha的alpha.xml文件编写方法(这些标签都是放在set标签中的)

 

Xml代码 复制代码 收藏代码
  1. <alpha android:fromAlpha="1.0"  
  2.     android:toAlpha="0.0"  
  3.     android:atartOffset="500"  
  4.     android:duration="500" />  
 

rotate.xml

 

Xml代码 复制代码 收藏代码
  1. <rotate android:fromDegrees="0"  
  2.     android:toDegrees="+350"         //正350度   
  3.     android:pivotX="50%"  
  4.     android:pivotY="50%"  
  5.     android:duration="3000" />  
 

这里要特别注意跟位置有关的参数pivotX和pivotY

3种写法对应3种相对位置方式的设置方式

android:pivotX="50" 绝对定位

android:pivotX="50%" 相对于控件本身

android:pivotX="50%p" 相对于父控件

 

translate.xml

 

Xml代码 复制代码 收藏代码
  1. <translate android:fromXDelta="50%"  
  2.     android:toXDelta="100%"  
  3.     android:fromYDelta="0%"  
  4.     android:toYDelta="100%"  
  5.     android:duration="1000" />  

 

scale.xml

Xml代码 复制代码 收藏代码
  1. <scale android:fromXScale="1.0"  
  2.     android:toXScale="0.0"  
  3.     android:fromYScale="1.0"  
  4.     android:toYScale="0.0"  
  5.     android:pivotX="50%"  
  6.     android:pivotY="50%"  
  7.     android:duration="2000" />  

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

Java代码 复制代码 收藏代码
  1. Animation animation=AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);  //载入布局文件  
  2. imageView.startAnimation(animation);  

Animations的使用(3)

 

1 AnimationSet的使用方法

什么是AnimationSet

1 AnimationSet是Animation的子类

2 一个AnimationSet包含了一系列的Animation

3 针对AnimationSet设置一些Animation的常见属性(如StartOffset,duration等),可以被包含在AnimationSet当中的Animation继承

使用步骤:(类似1中的例子 只不过含有2个动画效果)

 

Java代码 复制代码 收藏代码
  1. AnimationSet animationSet = new AnimationSet(ture);   
  2. AlpahaAnimation alpha = new AlphaAnimation(...);   
  3. RotateAnimation rotate = new RotateAnimation(...);   
  4. animationSet.addAnimation(alpha);   
  5. animationSet.addAnimaion(rotate);   
  6. animationSet.setDuration(2000);   
  7. animationSet.setStartOffset(500);   
  8. imageView.startAnimation(animationSet);  
 

 

2 Interpolator的使用方法

Interpolator定义了动画变化速率,在Animations框架中定义了以下几种Interpolator

 

AccelerateDecelerateInterpolator:在动画开始和结束的地方速率变化较慢,中间的时候加速

AccelerateInterpolator:在动画开始的地方速率改变较慢,然后加速

CycleInterpolator:动画循环播放特定次数,速率改变沿正弦曲线

DecelerateInterpolator:在动画开始的地方速率改变较慢,然后减速

LinearInterpolator:以均匀的速率改变

设置的地方就在set标签中的 android:interpolator="@android:anim/accelerate_interpolator"

而之后还有一个android:shareInterpolator="true" 从名字就可以看到这是为set中所有的动画设置Interpolator

如果要单独设置 则将shareInterpolator设为false 然后为每个动画中单独定义Interpolator

以上是在xml中设置,如果要在代码中设置

animationSet.setInterpolator(new AccelerateInterpolator());(也可以单独设置)

注意在AnimationSet的构造方法中有一个boolean参数,这个参数就是shareInterpolator的设定

3 Frame-By-Frame Animations的使用方法

1 在res/drawable中创建一个xml文件,定义Animation的动画播放序列 anim_nv.xml

Xml代码 复制代码 收藏代码
  1. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:oneshot="false">  
  3.     <item android:drawable="@drawable/nv1"  
  4.         android:duration="500" />  
  5.     <item android:drawable="@drawable/nv2"  
  6.         android:duration="500" />  
  7.     <item android:drawable="@drawable/nv3"  
  8.         android:duration="500" />  
  9.     <item android:drawable="@drawable/nv4"  
  10.         android:duration="500" />  
  11. </animation-list>  

2 为ImageView设置背景资源

Java代码 复制代码 收藏代码
  1. imageView.setBackgroundResource(R.drawable.anim_nv);  

3 通过ImageView得到AnimationDrawable

Java代码 复制代码 收藏代码
  1. AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();  

3 执行动画

Java代码 复制代码 收藏代码
  1. animationDrawable.start();  

Animations使用(4)

LayoutAnimationController的使用方法(与ListView结合使用为例)

什么是LayoutAnimationController

1 LayoutAnimationController用于为一个layout里面的控件,或者是一个ViewGroup里面的控件设置动画效果

2 每一个控件都有相同的动画效果

3 这些控件的动画效果在不同的时间显示出来

4 LayoutAnimationController可以在xml文件中设置,也可以在代码中设置

 

在XML中使用LayoutAnimaionController

1 在res/anim文件夹中创建一个文件,名为list_anim_layout.xml

 

Xml代码 复制代码 收藏代码
  1. <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:delay="0.5"  
  3.     android:animationOrder="random"  
  4.     android:animation="@anim/list_anim" />  

注意到list_anim这个xml文件,其中配置了动画效果,也就是一个动画配置文件(见3中)

<set>

<alpha...>

</set>

2 在布局文件中为ListView添加如下配置(就是在<listview>标签中添加一个属性)

android:layoutAnimation="@anim/list_anim_layout"

 

在代码中使用LayoutAnimationController

 

Java代码 复制代码 收藏代码
  1. 1 创建一个Animation对象:   
  2.     可以通过装载xml,也可以直接使用Animation的构造函数创建Animation对象   
  3. 2 创建LayoutAnimationController对象   
  4.     LayoutAnimationController lac=new LayoutAnimationController(animation);   
  5. 3 设置控件显示顺序   
  6.     lac.setOrder(LayoutAnimationController.ORDER_NORMAL);   
  7. 4 为ListView设置LayoutAnimationController属性:   
  8.     listView.setLayoutAnimation(lac);  

AnimationListener的使用方法

什么是AnimationListener

1 Animation是一个监听器

2 该监听器在动画执行的各个阶段会得到通知,从而调用相应的方法

3 主要包含下面的三个方法

onAnimationEnd(Animation animation)

onAnimationRepeat(Animation animation)

onAnimationStart(Animation animation)

使用方法:

animation.setAnimationListener(new XxxAnimationListener);

其中XxxAnimationListener继承AnimationListene

在其中实现三个onXXX方法


原创粉丝点击