android 动画-Tween Animation(一)动画体系简介,涉及到的核心类、核心接口讲解(上)

来源:互联网 发布:利用js实现动态时间 编辑:程序博客网 时间:2024/06/10 04:14
一、Tween Animation动画概述
    Tween(补间)动画可以对一个View执行一系列简单变换(位置、大小、旋转、透明度)。比如,你可以对一个ImageView进行改变大小、调整位置、放大缩小、调整透明度等变换。
    Tween动画既可以通过xml来实现,也可以通过代码来动态实现;写在xml中调用会相对慢些,但是复用性强,内容清晰明了;写在代码中,内容不易读,而且复用性差;
主要分为四类:(类图如下)

可以看到andorid.view.animation包下主要如下四类Animation:
TraslateAnimation:平移动画
ScaleAnimation:缩放动画
RotateAnimation:旋转动画
AlphaAnimation:透明度动画

两个核心类:
Animation:所有View动画类的抽象基类
AnimationSet: set of animations的概念,是一组动画的概念,是集合的概念。用于承载一组画的容器。

二、介绍这四类主要的动画和两个核心类;
1、Animation是动画类的基类,其中定义了一些动画的基础属性,及控制动画显示的一些基础的方法;常用的属性和方法如下:
            类

xml属性

java实现

解释

Animation

android:duration

setDuration(long)

动画持续时间,毫秒为单位

android:fillAfter

setFillAfter(boolean)

控件动画结束时是否保持动画最后的状态

android:fillBefore

setFillBefore(boolean)

控件动画结束时是否还原到开始动画前的状态

android:fillEnabled

setFillEnabled(boolean)

android:fillBefore效果相同

android:interpolator

setInterpolator(Interpolator)

设定插值器(指定的动画效果,譬如回弹等)

android:repeatCount

setRepeatCount(int)

重复次数

android:repeatMode

setRepeatMode(int)

重复类型有两个值,reverse表示倒序回放,restart表示从头播放

android:startOffset

setStartOffset(long)

调用start函数之后等待开始运行的时间,单位为毫秒


2、AlphaAnimation extends Animation; AlphaAnimation的主要属性都是通过构造函数传递,主要属性如下表:

AlphaAnimation

android:fromAlpha

AlphaAnimation(float fromAlpha, ...) 

 

动画开始的透明度,从0.0 --1.0 0.0表示全透明,1.0表示完全不透明

android:toAlpha

AlphaAnimation(...float toAlpha) 

动画结束的透明度,从0.0 --1.0 0.0表示全透明,1.0表示完全不透明


3、TranslateAnimation extends Animation,TranslateAnimation中主要的属性都是通过构造函数传递的;

这里简单解释下TranslateAnimation的三个构造函数:

TranslateAnimation(Context context,AttributeSet attrs):这个构造函数是通过xml写View动画的时候系统回调的,我们不用管。

TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,int fromYType, float fromYValue, int toYType, float toYValue)

通过函数的参数我们可以发现XXXType和XXXValue是一一对应的,看字面意思是说,每个Value值可以对应不同Value类型,没错,Value类型不同,ValueType的写法是不一样的;在Animation中给我们提供了三种ValueType,分别是:

public static final int ABSOLUTE = 0;

public static final int RELATIVE_TO_SELF = 1;

public static final int RELATIVE_TO_PARENT 2;

通过传入这三种value类型,具体的value的写法也是不一样的,实现效果也是不一样的:
valueType
value内容解释
ABSOLUTE 
50
绝对的像素值
RELATIVE_TO_SELF 
50%
相对于自身宽高的设置百分比,大小:0.0-1.0 浮点数
RELATIVE_TO_PARENT 
50%p
相对于父控件的宽高设置百分比,大小0.0-1.0 浮点数

TranslateAnimation(float fromXValue, float toXValue, float fromYValue,  float toYValue):这个函数中相对于上一个函数的valueType传值都是ABSOLUTE ,也就是传入的值都必须是绝对的像素值;


具体的属性如下:其中特别注意下四个属性的参数类型;

TranslateAnimation

android:fromXDelta

TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
        int fromYType, float fromYValue, int toYType, float toYValue)

起始点X轴坐标,可以是数值、百分数、百分数三种样式;比如 5050%50%p

android:toXDelta

TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
        int fromYType, float fromYValue, int toYType, float toYValue)

结束点X坐标,可以是数值、百分数、百分数三种样式;比如 5050%50%p

android:fromYDelta

TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
        int fromYType, float fromYValue, int toYType, float toYValue)

起始点Y轴从标,可以是数值、百分数、百分数三种样式比如 5050%50%p

android:toYDelta

TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
        int fromYType, float fromYValue, int toYType, float toYValue)

结束点Y轴坐标可以是数值、百分数、百分数三种样式;比如 5050%50%p


4、ScacleAnimation extends Animation;主要属性还是通过构造函数来实现的,具体如下:


ScaleAnimation

android:fromXScale

ScaleAnimation(float fromXfloat toXfloat fromY, float toY,
        float pivotX,float pivotY

 起始的X方向上相对自身的缩放比例,浮点值,比如1.0代表自身无变化,0.5代表起始时缩小一倍,2.0代表放大一倍;

android:toXScale

ScaleAnimation(float fromXfloat toXfloat fromY, float toY,
        float pivotX,float pivotY

结尾的X方向上相对自身的缩放比例,浮点值;

android:fromYScale

ScaleAnimation(float fromXfloat toXfloat fromY, float toY,
        float pivotX,float pivotY

起始的Y方向上相对自身的缩放比例,浮点值,

android:toYScale   

ScaleAnimation(float fromXfloat toXfloat fromY, float toY,
        float pivotX,float pivotY

结尾的Y方向上相对自身的缩放比例,浮点值;

android:pivotX

ScaleAnimation(float fromX, float toXfloat fromY, float toY,
        int pivotXType, float pivotXValue, int pivotYType, float pivotYValue

 缩放起点X轴坐标,可以是数值、百分数、百分数p三种样式,比如 5050%50%p;当为数值时,表示在当前控件的左上角加上50px,做为起始缩放点;如果是50%,表示在当前控件的左上角加上自己宽度的50%做为起始点;如果是50%p,那么就是表示在当前控件的左上角加上父控件宽度的50%做为起始点x轴坐标。

android:pivotY

ScaleAnimation(float fromX, float toXfloat fromY, float toY,
        int pivotXType, float pivotXValue, int pivotYType, float pivotYValue

缩放起点Y轴坐标,取值及意义跟android:pivotX一样。


涉及到的属性比较多,但是都比较好理解,相对麻烦点的地方就是缩放中心点的参数;这个参数还是有三种的赋值方式,具体
赋值的方式和含义与(3)中讲解TranslateAnimation是一致的,有不懂的看上面吧。
5、RotateAnimation extends Animation;主要属性还是通过构造函数来实现,具体内容如下:

RotateAnimation

android:fromDegrees

RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,
        int pivotYType, float pivotYValue)

 

开始旋转的角度位置,正值代表顺时针方向度数,负值代码逆时针方向度数

android:toDegrees

RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,
        int pivotYType, float pivotYValue)

 

结束时旋转到的角度位置,正值代表顺时针方向度数,负值代码逆时针方向度数

android:pivotX

RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,
        int pivotYType, float pivotYValue)

pivotXType默认ABSOLUTE

 缩放起点X轴坐标,可以是数值、百分数、百分数p三种样式,比如 5050%50%p

android:pivotY 

RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,
        int pivotYType, float pivotYValue)

pivotYType默认ABSOLUTE

 缩放起点Y轴坐标,可以是数值、百分数、百分数p三种样式,比如 5050%50%p

其中也涉及到了pivotX和pivotY的写法和实现,具体解释同ScacleAnimation 中的解释,不再赘述。
6、AnimationSet类:
这个类是一个容器类,主要用来实现组合动画;组合动画的实现方式有两种(单一动画的实现方式也是这两种);
(1)通过xml的方式实现;
xml放置在res/anim/目录下,如果res文件下没有anim目录,就之间新建一个;
动画类与xml标签的对应关系如下表:
                                                                标签对应

AnimationSet

<set></set>

AlphaAnimation

<alpha></alpha>

RotateAnimation

<rotate></rotate>

TranslateAnimation

<translate></translate>

ScalceAnimation

<scale></scale>


简单模板:
xml实现
<set
    android:interpolator=""
    android:shareInterpolator="">

    <scale "通用属性"+“专有属性”/>

    <rotate "通用属性"+“专有属性”/>

    <alpha "通用属性"+“专有属性”/>

    <translate "通用属性"+“专有属性”/>

    <set>

        <scale/>
        
        ...

    </set>
</set>

    xml中涉及到的四类动画的属性我不再赘述,在上文均有提到,其中通用属性是指Animation的属性,专有属性是指每个XXXAnimaiton自己的属性;
    set中的属性介绍下,set中一共两个属性,interpolator和shareInterpolator;interpolator属性用于设置整个动画集的插值器,shareInterpolator是这个插值器的开关,如果shareInterpolator=true,则整个动画集的动画都按照指定的插值器来进行动画操作,如果shareInterpolator=false,则指定的动画插值器将不起作用,可以再针对具体的动画标签来设置动画插值器;至于动画插值器是什么,稍后补上,这里先记住这两个属性及其关系即可。

java代码实现;
   AnimationSet animtionSet = AnimationUtils.loadAnimation(context,R.anim.XXX);//加载xml中的动画布局
   animationSet.setXXX();//为动画集设置公用的属性,可以设置多个
   XXXview.startAnimation(animationSet);//启动动画

(2)通过代码动态实现;
TranslateAnimation translate = new TranslateAnimation(xx,xx,xx,xx);//定义平移动画
AlphaAnimation alpha = new AlpahAnimation(xx,xx);//定义透明度动画   这里只是随意举例子
AnimationSet animationSet = new Animation(boolean);//注意,这里的true,false和xml中设置的shareInterpolator是等价的;

translate.setXXX();//设置translate动画属性
alpha.setXXX();//设置alpha动画属性
animationSet.setXXX();//设置动画集通用的属性
animationSet.addAnimation(translate);
animationSet.addAnimation(alpha);//具体动画添加到集合中
XXXview.StartAnimation(animationSet);//启动动画

注意:动画集中的动画可以同时开始动画,也可以先后开始动画,具体按照哪种方式执行,分别通过在AnimationSet和XXXAnimation中执行setDuration方法和setStartOff方法控制即可,当然,也可以通过xml来设置这两个属性。

由于文章篇幅过长,其他未讲到的内容请看下一节;android 动画-Tween Animation(一)动画体系简介,涉及到的核心类、核心接口讲解(下)

















0 0
原创粉丝点击