Android中的动画概述

动画可以分为三类:View动画,帧动画,属性动画。

一、View动画

1.View动画包括四种:平移动画,缩放动画,旋转动画,透明度动画。分别对应着Animation的四个子类:TranslateAnimation,ScaleAnimation,RotateAnimation,AlphaAnimation。帧动画其实也是View动画的一种。

2.View动画是对View的影像做动画,并不是真正的改变View的位置等状态。通过View动画将View平移后,新位置不能触发点击事件,老位置可以。

3.使用View动画,在动画完成后,有时候会出现View无法隐藏的现象,即设置setVisiable(View.GONE)没有效果,这个时候只需要调用view.clearAnimation()清除View动画即可解决此问题。

4.View动画的使用:

通过XMl定义:Animation animation = AnimationUtils.loadAnimation(context,resId);

view.startAnimation(animation);

通过纯代码:

AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);

alphaAnimation.setDuration(300);

view.startAnimation(alphaAnimation);

为动画设置监听:

animation.setAnimationListener(new Animation.AnimationListener());

5.自定义View动画:

继承Animation类,重写initialize和applyTransformation方法,在initialize方法中做一些初始化工作,在applyTransformation中进行相应的矩阵变换。很多时候需要用到Camera来简化矩阵变换的过程,需要用到数学上面的东西。实际开发中很少用到自定义View动画。

二、帧动画

1.帧动画是顺序播放一组预先定义好的图片,类似于电影播放。对应于AnimationDrawable类。

XML中的定义方式如下(在drawable文件夹中):

然后将上诉Drawable作为View的背景并通过AnimationDrawable来播放:

view.setBackgroundResource(R.drawable.resid);

AnimationDrawable drawable = view.getBackground();

drawable.start();

2.帧动画使用比较简单,但是比较容易引起OOM,所以在使用帧动画时应尽量避免使用过多尺寸较大的图片。

3.View动画的一个特殊使用场景,就是为ViewGroup中的所有子元素添加出场效果,需要用到LayoutAnimation。

定义LayoutAnimation:

android:delay="0.5"

android:animationOrder="normal"

android:animation="@anim/anim_item">

其中anim_item就是具体的View动画,然后为ViewGroup指定android:layoutAnimation属性 android:layoutAnimation=“@anim/layout_animation”;

4.除了在XML中指定LayoutAnimation外,还可以通过LayoutAnimationController来实现:

Animation animation = AnimationUtils.loadAnimation(context,resid);

LayoutAnimationController controller = new LayoutAnimationController(animation);

controller.setDelay(0.5f);

controller.setOrder(LayoutAnimationController.ORDER_NORMAL);

viewGroup.setLayoutAnimation(controller);

5.Activity的切换效果:

调用overridePendingTransition(resid,resid),但是必须在startActivity跟finish之后调用才有效果!

三、属性动画

1.属性动画是API 11(3.1) 新加入的特性,和View动画不同,它对作用对象进行了扩展,属性动画可以对任何对象做动画,甚至还可以没有对象。

动画默认时间间隔300ms,默认帧率10ms/帧。其可以达到的效果是:在一个时间间隔内完成对象从一个属性值到另一个属性值的改变。

2.比较常用的几个动画类:ValueAnimator,ObjectAnimator,AnimatorSet。其中ObjectAnimator继承自ValueAnimator,AnimatorSet和ValueAnimator都继承自Animator。

AnimatorSet是个动画集合,可以定义一组动画。

3.Animator是个抽象类,实现了Cloneable接口,Cloneable接口里面什么都没有。

4.ValueAnimator不能对具体的某个属性做动画,但是可以对一个值做动画,就是实现值的渐变。例如:ValueAnimator.ofInt(1.100)。具体的对某个属性值的动画的实现,在它的子类ObjectAnimator中。不过可以通过ValueAnimator对值的渐变,结合AnimatorUpdateListener可以做很多事情(包括实现属性动画)。

5.使用方法示例:

通过代码:ObjectAnimator.ofFloat(view,"propertyName",value).start();

通过XML:属性动画需要定义在res/animator/目录下面,语法如下:

android:ordering="together">

android:propertyName="x"

android:duration="300"

android:valueTo="200"

android:valueType="floatType"/>

android:propertyName="y"

android:duration="300"

android:valueTo="300"

android:valueType="floatType"/>

一个下面可以包含多个。

AnimatorSet set = AnimatorSet.loadAnimator(context,resid);

set.setTarget(view);

set.start();

6.插值器与估值器

插值器:根据时间流逝的百分比来计算出当前属性值改变的百分比。TimeInterpolator

系统预置的有LinearInterpolator(线性插值器:匀速动画)、AccelerateDecelerateInterpolator(加速减速插值器:动画两头慢,中间快)、DecelerateInterpolator(减速插值器:动画越来越慢)等。

这些插值器都继承自BaseInterpolator,BaseInterpolator实现了Interpolator接口,而Interpolator几口继承自TimeInterpolator接口。TimeInterpolator接口定义一个方法叫:

public float getInterpolation(float intput); 所以自定义插值器的话,只需要实现Interpolator或者TimeInterpolator接口。

估值器:根据当前属性改变的百分比来计算改变后的属性值。TypeEvaluator

系统预置的估值器有IntEvaluator(针对整型属性)、FloatEvaluator(针对浮点型)、ArgbEvaluator(针对Color属性)。

它们都实现了TypeEvaluator接口,TypeEvaluator接口中定义了一个方法:

public T evaluate(float fraction, T startValue, T endValue);

7.属性动画要求对象的该属性必须有set方法,get方法可选(如果提供了动画的初始值,则不需要get方法,若没有提供初始值,那么系统会调用get方法获取初始值)。

8.对任意属性做动画:如果对象没有该属性的set或者get方法,可以通过如下3种方式解决:

①:给对象加上get和set方法,如果有权限的话。

②:用一个类来包装原始对象,间接为其提供get,set方法。(此方法比较常用)

③:采用ValueAnimator,监听动画过程,自己实现属性的改变。

关于方法2的例子:

例如如果对Button的width属性做动画,但是Button的width的set方法是设置最大跟最小宽度,而不是改变其宽度。get方法是获取宽度。这样的话直接对width属性做动画是没有效果的,因为根本不是设置的那个我们想改变的宽度。这样我们可以通过一个类去包装Button,然后在这个类的setWidth方法中,去

button.getLayoutParams().width = width.

button.requestLayout();

这样就间接的给Button的宽度做了动画。因为直接为这个宽度去加set方法是行不通的,Button封装在SDK中,我们改变不了。

9.在动画的内部,get,set方法的调用是通过反射来实现的。

注意:属性动画必须运行在有Looper的线程中,否则会抛出“Animators may only be run on Looper threads”异常。

你可能感兴趣的:(Android中的动画概述)