android 属性动画封装,Android 属性动画

本文介绍了Android 3.0引入的属性动画,包括ValueAnimator的基础用法、ObjectAnimator的特性,以及如何通过XML资源文件和代码实现动画效果。重点讲解了ValueAnimator的创建、属性设置和监听器应用,以及ObjectAnimator在实际开发中的常见应用场景和注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

属性动画实(Property Animation)在 Android 3.0 中引入的,在补间动画中改变的只是 View 的绘制效果,而 View 的真实属性是没有变化的,而属性动画则可以直接改变 View 对象的属性值,属性动画几乎可以对任何对象执行动画,而不是局限在 View 对象上,从某种意义上讲,属性动画可以说是增强版的补间动画。

动画属性

说明

动画持续时间

默认值是 300 ms,在资源文件中通过 android:duration 指定

动画插值方式

在资源文件中通过 android:interpolator 指定

动画重复次数

指定动画重复的次数,在资源文件中通过 android:repeatCount 指定

动画重复模式

指定一次动画播放结束后,结束下次动画时,是从开始帧再次播放到结束帧,还是从结束帧反向播放到开始帧,在资源文件中通过 android:repeatMode 指定

帧刷新频率

指定间隔多长时间播放一帧,默认值是 10 ms

动画集合

通过动画集合将多个属性动画组合起来,通过指定 android:ordering 属性控制这租动画是按次序播放还是同时播放,在资源文件中通过 来表示

属性动画的基类是 Animator,是一个抽象类,所以不会直接使用这个类,通常通过继承它并重写其中的方法,Android SDK 默认提供了几个子类,大多数情况使用这些子类就能完成日常开发。

Evaluator

Evaluator 是用来控制属性动画如何计算属性值。它的接口定义是 TypeEvaluator,其中定义了 evaluate 方法,供不同类型的子类实现。

public interface TypeEvaluator {

T evaluate(float var1, T var2, T var3);

}

常见的实现类有 IntEvaluator、FloatEvaluator、ArgbEvaluator 等。

AnimatorSet

AnimatorSet 是 Animator 的子类,用来组合多个 Animator,并指定这些 Animator 是顺序播放还是同时播放。

ValueAnimator

ValueAnimator 是属性动画最重要的一个类,继承自 Animator。定义了属性动画大部分的核心功能,包括计算各个帧的属性值、处理更新事件、按照属性值的类型控制计算规则等。

一个完整的属性动画由以下两部分组成。

计算动画各个帧的相关属性值。

将这些属性值设置给指定的对象。

ValueAnimator 实现了第一部分的功能。ValueAnimator的构造函数是空实现,一般都是使用静态工厂方法来实现实例化。

/**

* Creates a new ValueAnimator object. This default constructor is primarily for

* use internally; the factory methods which take parameters are more generally

* useful.

*/

public ValueAnimator() {

}

/**

* Constructs and returns a ValueAnimator that animates between int values. A single

* value implies that that value is the one being animated to. However, this is not typically

* useful in a ValueAnimator object because there is no way for the object to determine the

* starting value for the animation (unlike ObjectAnimator, which can derive that value

* from the target object and property being animated). Therefore, there should typically

* be two or more values.

*

* @param values A set of values that the animation will animate between over time.

* @return A ValueAnimator object that is set up to animate between the given values.

*/

public static ValueAnimator ofInt(int... values) {

ValueAnimator anim = new ValueAnimator();

anim.setIntValues(values);

return anim;

}

/**

* Constructs and returns a ValueAnimator that animates between color values. A single

* value implies that that value is the one being animated to. However, this is not typically

* useful in a ValueAnimator object because there is no way for the object to determine the

* starting value for the animation (unlike ObjectAnimator, which can derive that value

* from the target object and property being animated). Therefore, there should typically

* be two or more values.

*

* @param values A set of values that the animation will animate between over time.

* @return A ValueAnimator object that is set up to animate between the given values.

*/

public static ValueAnimator ofArgb(int... values) {

ValueAnimator anim = new ValueAnimator();

anim.setIntValues(values);

anim.setEvaluator(ArgbEvaluator.getInstance());

return anim;

}

/**

* Constructs and returns a ValueAnimator that animates between float values. A single

* value implies that that value is the one being animated to. However, this is not typically

* useful in a ValueAnimator object because there is no way for the object to determine the

* starting value for the animation (unlike ObjectAnimator, which can derive that value

* from the target object and property being animated). Therefore, there should typically

* be two or more values.

*

* @param values A set of values that the animation will animate between over time.

* @return A ValueAnimator object that is set up to animate between the given values.

*/

public static ValueAnimator ofFloat(float... values) {

ValueAnimator anim = new ValueAnimator();

anim.setFloatValues(values);

return anim;

}

/**

* Constructs and returns a ValueAnimator that animates between the values

* specified in the PropertyValuesHolder objects.

*

* @param values A set of PropertyValuesHolder objects whose values will be animated

* between over time.

* @return A ValueAnimator object that is set up to animate between the given values.

*/

public static ValueAnimator ofPropertyValuesHolder(PropertyValuesHolder... values) {

ValueAnimator anim = new ValueAnimator();

anim.setValues(values);

return anim;

}

/**

* Constructs and returns a ValueAnimator that animates between Object values. A single

* value implies that that value is the one being animated to. However, this is not typically

* useful in a ValueAnimator object because there is no way for the object to determine the

* starting value for the animation (unlike ObjectAnimator, which can derive that value

* from the target object and property being animated). Therefore, there should typically

* be two or more values.

*

*

Note: The Object values are stored as references to the original

* objects, which means that changes to those objects after this method is called will

* affect the values on the animator. If the objects will be mutated externally after

* this method is called, callers should pass a copy of those objects instead.

*

*

Since ValueAnimator does not know how to animate between arbitrary Objects, this

* factory method also takes a TypeEvaluator object that the ValueAnimator will use

* to perform that interpolation.

*

* @param evaluator A TypeEvaluator that will be called on each animation frame to

* provide the ncessry interpolation between the Object values to derive the animated

* value.

* @param values A set of values that the animation will animate between over time.

* @return A ValueAnimator object that is set up to animate between the given values.

*/

public static ValueAnimator ofObject(TypeEvaluator evaluator, Object... values) {

ValueAnimator anim = new ValueAnimator();

anim.setObjectValues(values);

anim.setEvaluator(evaluator);

return anim;

}

获得实例之后,接着需要设置动画持续时间、插值方式、重复次数等属性值,然后启动动画,最后需要为 ValueAnimator 注册 AnimatiorUpdateListener 监听器,并在监听器的 onAnimationUpdate 方法中将计算出来的属性值设置给指定对象。

ObjectAnimator

ObjectAnimator 是 ValueAnimator 的子类,封装实现了属性动画的第二部分的功能。

实际开发中用得最多的就是 ObjectAnimator,只有在 ObjectAnimator 实现不了的场景下,才考虑使用 ValueAnimator。ObjectAnimator 和 ValueAnimator 在构造实例时最大的不同是需要指定动画作用的具体对象和对象的属性名,而且一般不需要注册 AnimatiorUpdateListener 监听器。

ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(this, "progress", progress);

objectAnimator.setDuration(300);

objectAnimator.start();

使用 ObjectAnimator 有以下几点需要注意。

需要为对象对应的属性提供 setter 方法。

动画的对象是 View 的话,为了显示动画效果,在某些情况下,需要注册 AnimatiorUpdateListener 监听器,在其回调方法 onAnimationUpdate 中调用 View 的 invalidate 方法来刷新 View 的显示。

XML 资源文件方式

属性动画 XML 方式实现。

在项目中的 res/animator 目录中存放这个属性动画的 XML 文件。

android:ordering="together">

android:duration="2000"

android:propertyName="scaleX"

android:valueFrom="1"

android:valueTo="0.4"

android:valueType="floatType" />

android:duration="2000"

android:propertyName="scaleY"

android:valueFrom="1"

android:valueTo="0.4"

android:valueType="floatType" />

使用代码加载。

ivFrameXML.setImageResource(R.drawable.animation);

AnimationDrawable anim = (AnimationDrawable) ivFrameXML.getDrawable();

anim.start();

代码方式

AnimatorSet animatorSet = new AnimatorSet();

ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(this, "scaleX", 1F, 0.4F);

ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(this, "scaleY", 1F, 0.4F);

animatorSet.playTogether(objectAnimator, objectAnimator1);

animatorSet.setDuration(2000);

animatorSet.setTarget(ivPropertyCode);

animatorSet.start();

实现效果

b3aa15c902a3

实现效果.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值