Android中的属性动画

来源:互联网 发布:逻辑门电路实验数据 编辑:程序博客网 时间:2024/05/22 06:44

Android中的动画分为三类

  • view animatiom
    包括scale, alpha, rotate, translate。也叫补间动画。

  • drawable animation
    也就是帧动画,按照顺序播放图片资源。

  • property animation
    属性动画,在android 3.0,也就是API 11之后引入。比view animation要更强大。

以下是官网对属性动画Property Animation的概叙:

The property animation system is a robust framework that allows you to animate almost anything. You can define an animation to change any object property over time, regardless of whether it draws to the screen or not. A property animation changes a property’s (a field in an object) value over a specified length of time. 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.

属性动画的使用很简单,比如下面这个例子:

public void rotateyAnimRun(View view) {    ObjectAnimator.ofFloat(view, "rotationX", 0.0F, 180.0F, 0.0F)            .setDuration(2000)            .start();}public void onClick(View v) {    switch (v.getId()) {        case R.id.img:            rotateyAnimRun(v);            break;

也就是点击了一个图片之后会在这个图片上出现一些动画效果。效果如下
这里写图片描述

这个属性动画意思是操作view的rotationX属性,先变成0.0F,再变成180.0F,再变成0.0F。setDuration自然就是设置这段动画持续的时间。

属性动画大概的过程如下

Created with Raphaël 2.1.0开始ValueAnimator计算出表示当前过去的时间和总时间的比值TimeInterpolator根据上面的值计算出另一个值,表示当前进度TypeEvaluator根据上面的值和start/end value计算出当前属性真正的值

上面的例子中的ObjectAnimator就是ValueAnimator的一个实现类。TimeInterpolator,这个类可以控制动画在各个时段的速度。上面是动画是匀速的,也可以创建出变化速度先快后慢,或者类似重力加速度的动画效果。

和View Animation像比较,Property View有好些优点。
View Animation的动画效果只是效果,并没有真正的改变view的属性,比如说位置。而Property View是确实改变了属性。而且View Animation只能用在View上,而Property View可以用在其他不是View的子类的类上。View Animation只能做出某些属性上的效果,Property View则没有这个限制。

0 0