欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android动画开发常见属性解析

程序员文章站 2023-01-29 20:56:37
android动画开发常见属性解析 在android官方文档中把动画分为三种: - property animation 在3.0以后引进的,可以调整任何对象的属性来实现动画...

android动画开发常见属性解析

在android官方文档中把动画分为三种:
- property animation
在3.0以后引进的,可以调整任何对象的属性来实现动画(the property animation system lets you animate properties of any object),包括尚未显示的视图控件,并且可扩展,可以用于自定义view,是推荐使用的动画实现方式;
- view animation:
只能作用于view对象上,也被称作补间动画(补间动画)。能够在view的内容上实现一些简单的变化效果(position, size, rotation, and transparency),有xml和android代码两种实现方式
- drawable animation
使用一个一个的drawable资源来实现动画,即按次序显示图片,使其持续一定的时间类似于帧动画。

属性动画(property anamation/animator)

to animate something, you specify the object property that you want to animate, such as an object’s position on the screen, how long you want to animate it for, and what values you want to animate between.

要实现一个动画,需要指定(要改变的)对象属性,比如在屏幕上的位置,持续的时间,和要变化的值。
所有可设置的项目:
- 变化的属性,不固定,可以通过代码详细设置多个属性值等;
- 变化的值,可以为多个,使变化更复杂;
- duration 持续时间,默认为300ms;
- time interpolation 时间插值:值以怎样的节奏变化;
- repeat count and behavior 重复及重复行为:是否重复,重复多少次,重复时是倒着来还是回到原点再来一遍;
- animator sets 动画集:可以在多个属性上设置变化,构成复杂的动画;
- frame refresh delay 帧率刷新频率,默认为10ms,极限取决于;

对于以上的一些数值,可以使用以下的类进行更*的设置:
- evaluator 转换器,设置变化的值:根据传入的数字进行需要的转化最后得到属性变化的值,也就是animator使用的ofint,offloat在背后使用的那种,也可以自己编写自定义的evaluator。因为 ofint 和 offloat 都是系统直接提供的函数,所以在使用时都会有默认的加速器和 evaluator 来使用的,不指定则使用默认的,ofint()的默认 evaluator 当然是 intevaluator,而 offloat() 默认的则是 floatevalutor;
- interpolator 插值器/加速器,设置tenter code hereime interpolation:在给定的值变化和时间范围内,以怎样的规律变化,系统提供的有以下这些。

valueanimator

valueanimator获取一组值,设置好evaluator,interpolator和duration(或使用默认的),就可以开始了,但是此时仅仅是对值进行计算和变化而已,要实现动画,还需要添加animatorupdatelistener并且实现其中的onanimationupdate方法,该方法在动画每一帧刷新时调用,在这个方法中使用值更新控件,不断改变控件的属性值,实现动画效果。

值的获取

valueanimator提供几个方法来获取值的变化范围,对于argb值,float,int都有默认的方法(以及相对应的evaluator)
几个方法都不提供默认初始值,所以参数至少要两个,当然可以更多,变化就更复杂;
- ofargb(int... values)
- offloat(float... values)
- ofint(int... values)
- ofobject (typeevaluator evaluator, object... values):不确定值的数据类型,需要指定一个evaluator

设置动画

valueanimator animation = valueanimator.offloat(0f, 100f);
//valueanimator animation = valueanimator.ofobject(new mytypeevaluator(), startpropertyvalue, endpropertyvalue);
animation.setduration(1000);
animation.addupdatelistener(new valueanimator.animatorupdatelistener() {
    @override
    public void onanimationupdate(valueanimator updatedanimation) {
        // you can use the animated value in a property that uses the
        // same type as the animation. in this case, you can use the
        // float value in the translationx property.
        float animatedvalue = (float)updatedanimation.getanimatedvalue();
        textview.settranslationx(animatedvalue);
    }
});
animation.start();

objectanimator

objectanimator是valueanimator的子类,使用起来更加方便快捷,但是对使用场景有一定的要求。
objectanimator的of方法中,除了设定值之外,还需要直接指定动画作用的控件,和值作用的属性,而且这个属性必须在控件的类中有set方法可以设置值,即把valueanimator中的updatelistener简化了

objectanimator animator = objectanimator.offloat(tv,"alpha",1,0,1);  
animator.setduration(2000);  
animator.start();

这样设置就可以直接实现动画了。

animatorset

对于多个属性值变化的动画,可以编写多个objectanimator,然后使用animatorset进行控制。

animatorset bouncer = new animatorset();
bouncer.play(bounceanim).before(squashanim1);
bouncer.play(squashanim1).with(squashanim2);
bouncer.play(squashanim1).with(stretchanim1);
bouncer.play(squashanim1).with(stretchanim2);
bouncer.play(bouncebackanim).after(stretchanim2);
// 全部一起运行
bouncer.playtogether(bounceanim, squashanim1, squashanim2, stretchanim1, stretchanim2, bouncebackanim);
valueanimator fadeanim = objectanimator.offloat(newball, "alpha", 1f, 0f);
fadeanim.setduration(250);
animatorset animatorset = new animatorset();
animatorset.play(bouncer).before(fadeanim);
animatorset.start();

xml定义动画

文件放在res/animator/文件夹下
语法:



    

    

    
        ...
    

监听器

onanimationstart() - called when the animation starts. onanimationend() - called when the animation ends. onanimationrepeat() - called when the animation repeats itself. onanimationcancel()- called when the animation is canceled. a cancelled animation also calls onanimationend(), regardless of how they were ended.

传统动画(view animation/tweened animation,3.0以前)

view animation可以使用xml定义,也可以编写android代码,但一般推荐使用xml,因为更易读并且可以重复使用。

xml定义动画

每一个动画需要定义动画动作,什么时候开始,持续多长时间,这些动画可以是顺序发生的,也可以是同时进行的。
每个动画需要设置一系列的参数(大小,旋转角度,位置等起始值,结束值,起始时间,持续时间,插值器等)。
动画xml文件放置在res/anim/文件夹下,只能有一个root标签,可以是, , , 中的某一个,也可以是。默认情况下定义的所有动画会同时运行,需要顺序进行的话要自行使用startoffset属性定义。
例子:


    
    
        
        
    

在代码中设置xml动画:

animation animation = animationutils.loadanimation(mcontext, r.anim.alpha_anim);
img = (imageview) findviewbyid(r.id.img);
img.startanimation(animation);

在xml中定义的属性同样也可以使用代码设置。
所有的animation有一些通用的属性。
除此之外每种动画根据它们的特征有一些特定的属性。

alphaanimation

控制透明度变化的动画。
有两个属性表示透明度变化的起始值fromalpha和结束值toalpha。
xml语法:


代码控制:

alphaanimation am = new alphaanimation(0f, 1f);
am.setduration(1000);
mview.startanimation(am);

scaleanimation

控制尺寸变化的动画。
主要参数如下:
- fromx/tox:开始和结束时x的缩放比,0为不显示,1为原大小
- fromy/toy:开始和结束时y的缩放比,0为不显示,1为原大小
- pivotx/pivoty:进行缩放时的基准点,即以这个点为固定位置进行缩放,该点位置不变,0为左上,以百分数表示(使用代码可以设置pivotxtype等值,以view本身之外的参照物为基准点,此处咱不讨论)

xml语法:

代码语法和上面其他动画类似,根据参数的不同有不同的构造方法。

translateanimation

控制位置变化的动画。 主要属性: - fromx/tox:开始/结束时的x值 - fromy/toy:开始/结束时的y值

这些值同样也可以指定不限于view本身的参照物。 xml语法:

rotateanimation

控制旋转的动画。 主要属性: - fromdegrees/todegrees:旋转起始/结束时的偏移角度; - pivotx/pivoty:旋转的基准点,同样可以用pivotx/ytype设置参照物

xml语法:

animationset

协调多个动画的行为。 xml语法:

    ...
    
        ...
    

其中:interpolator即插值器,上面说过;shareinterpolator指是否把插值器应用到所有的子动画上,值是布尔型。

代码语法:

animationset as = new animationset(false);
as.addanimation(alphaani);
as.addanimation(scaleani);
...
as.setduration(1000);
mview.startanimation(as);

监听器

new animation.animationlistener() {
   @override
    public void onanimationstart(animation animation) {
        // todo auto-generated method stub

    }

    @override
    public void onanimationrepeat(animation animation) {
        // todo auto-generated method stub

    }

    @override
    public void onanimationend(animation animation) {
        // todo auto-generated method stub

    }
}

帧动画(drawable animation/frame animation)

和其他动画不同,drawable animation的资源文件放在res/drawable/中:


    
    
    

其中如果onshot设置为true,动画运行一次并停留在最后一个场景,如果为false,则循环播放。
使用时:

// load the imageview that will host the animation and
 // set its background to our animationdrawable xml resource.
 imageview img = (imageview)findviewbyid(r.id.spinning_wheel_image);
 img.setbackgroundresource(r.drawable.spin_animation);

 // get the background, which has been compiled to an animationdrawable object.
 animationdrawable frameanimation = (animationdrawable) img.getbackground();

 // start the animation (looped playback by default).
 frameanimation.start();