ObjectAnimator之android:propertyName

来源:互联网 发布:淘宝皇冠转让 编辑:程序博客网 时间:2024/06/16 18:58

在Android API level 11之后属性动画为我们实现动画提供了很大的便利。

ObjectAnimator anim = ObjectAnimator.ofFloat(foo, "alpha", 0f, 1f);anim.setDuration(1000);anim.start();

这样短短3行代码就实现了一个View的淡出动画(背景颜色由浅入深)。

动画无非就是在人眼识别的范围内,在单位时间显示多张图片。为了使整体上看的画面有连续性,这单位时间内一幅幅的图片内容的不同变化就构成了不同的动画效果。Android为了方便我们开发,在动画框架中提供了一些回调用让我可以控制动画过程的快慢,以及每一帧该如何计算,以及到底改变的是目标对象何种属性。

其中ObjectAnimator anim = ObjectAnimator.ofFloat(foo, “alpha”, 0f, 1f);第二个参数就决定了该改变哪种属性。

frameworks/base/core/java/android/animation/PropertyValuesHolder.java (android-5.1.1_r1)
这个类是动画的实现类之一,其中:

void setAnimatedValue(Object target);void setupSetter(Class targetClass);static String getMethodName(String prefix, String propertyName);

这几个函数是实现的关键,如果也有在研究这方面内容的人希望注意一下,这个就解释了:
The object property that you are animating must have a setter function (in camel case) in the form of set(). Because the ObjectAnimator automatically updates the property during animation, it must be able to access the property with this setter method. For example, if the property name is foo, you need to have a setFoo() method.

0 0