标签:android 动画 propertyanimation objectanimator
简述:
能不用PropertyAnimation就不用,如果只是做View就用ViewPropertyAnimator多个属性一起改。ViewAnimation并没有改变View的属性,点击还是原来区域。
ViewAnimation有很强的局限性:只能操作View对象、View的部分属性(不包含背景色)、只是改变View在哪里绘制,没有改变相应逻辑(比如Button移动,你点击的地方仍旧是以前的,相应逻辑需要你自己写)。
因为ViewAnimation改变View的绘制,所以是通过操控他的父View,ViewGroup来完成的,因为View本身没有这样的属性。所以View对象本身没有变化,导致View的行为操作,仍旧停留在原来区域。3.0后添加了get、set方法,就行bug修复。
而PropertyAnimation可以View对象的真实属性(调用方法),所以View会自动调用invalidate方法(View set方法会调)
然而ViewAnimation体系启动时间更少、需要代码更少。所以了ViewAnimation够用了,就别用PropertyAnimation。
获取数值的API是getAnimatedValue()
ValueAnimator animation = ValueAnimator.ofFloat(0f, 1f); animation.setDuration(1000); animation.start();
ValueAnimator animation = ValueAnimator.ofObject(new MyTypeEvaluator(), startPropertyValue, endPropertyValue); animation.setDuration(1000); animation.start();
ObjectAnimator anim = ObjectAnimator.ofFloat(foo, "alpha", 0f, 1f); anim.setDuration(1000); anim.start();根据不同的属性值,你可能需要调用invalidate方法,在
onAnimationUpdate()
这个回调中调用。例如Drawable对象的color属性,就只有你主动调用才刷新。而View类的所有带set***方法的属性***都会自己主动刷新,就不用你再调用了。
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); ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f); fadeAnim.setDuration(250); AnimatorSet animatorSet = new AnimatorSet(); animatorSet.play(bouncer).before(fadeAnim); animatorSet.start();
对ViewGroup布局的改变:
使用LayoutTransition
APPEARING
- A flag indicating the animation that runs on items that are appearing in the container.CHANGE_APPEARING
- A flag indicating the animation that runs on items that are changing due to a new item appearing in the container.DISAPPEARING
- A flag indicating the animation that runs on items that are disappearing from the container.CHANGE_DISAPPEARING
- A flag indicating the animation that runs on items that are changing due to an item disappearing from the container.
使用Interpolator:
加速器提供时间和返回值之间的映射关系。自己写就继承TimeInterpolator,或者用android.view.animation包里的。
AccelerateDecelerateInterpolator
public float getInterpolation(float input) { return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f; }
监听器:实现接口Animator.AnimatorListener,或者有空方法让你重写的AnimatorListenerAdapter类。
使用自定义的TypeEvaluator:
public class FloatEvaluator implements TypeEvaluator { public Object evaluate(float fraction, Object startValue, Object endValue) { float startFloat = ((Number) startValue).floatValue(); return startFloat + fraction * (((Number) endValue).floatValue() - startFloat); } }
监听关键的帧:
Keyframe kf0 = Keyframe.ofFloat(0f, 0f); Keyframe kf1 = Keyframe.ofFloat(.5f, 360f); Keyframe kf2 = Keyframe.ofFloat(1f, 0f); PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe("rotation", kf0, kf1, kf2); ObjectAnimator rotationAnim = ObjectAnimator.ofPropertyValuesHolder(target, pvhRotation) rotationAnim.setDuration(5000ms);
新增API中的属性:
translationX
and translationY
: These properties control where the View is located as a delta from its left and top coordinates which are set by its layout container.rotation
, rotationX
, and rotationY
: These properties control the rotation in 2D (rotation
property) and 3D around the pivot point.scaleX
and scaleY
: These properties control the 2D scaling of a View around its pivot point.pivotX
and pivotY
: These properties control the location of the pivot point, around which the rotation and scaling transforms occur. By default, the pivot point is located at the center
of the object.x
and y
: These are simple utility properties to describe the final location of the View in its container, as a sum of the left and top values and translationX and translationY values.alpha
: Represents the alpha transparency on the View. This value is 1 (opaque) by default, with a value of 0 representing full transparency (not visible).例子
ViewPropertyAnimator:
对比:
Multiple ObjectAnimator objects
ObjectAnimator animX = ObjectAnimator.ofFloat(myView, "x", 50f); ObjectAnimator animY = ObjectAnimator.ofFloat(myView, "y", 100f); AnimatorSet animSetXY = new AnimatorSet(); animSetXY.playTogether(animX, animY); animSetXY.start();
One ObjectAnimator
PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("x", 50f); PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", 100f); ObjectAnimator.ofPropertyValuesHolder(myView, pvhX, pvyY).start();
ViewPropertyAnimator
myView.animate().x(50f).y(100f);
使用XML:
为了区分ViewAnimation,需要在res/animator/目录取代res/anim/目录,要预览的话,系统只会在上述路径下扫描PropertyAnimation
ValueAnimator
- <animator>
ObjectAnimator
- <objectAnimator>
AnimatorSet
- <set>
<set android:ordering="sequentially"> <set> <objectAnimator android:propertyName="x" android:duration="500" android:valueTo="400" android:valueType="intType"/> <objectAnimator android:propertyName="y" android:duration="500" android:valueTo="300" android:valueType="intType"/> </set> <objectAnimator android:propertyName="alpha" android:duration="500" android:valueTo="1f"/> </set>
AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext, R.anim.property_animator); set.setTarget(myObject); set.start();
Android PropertyAnimation官网文档翻译
标签:android 动画 propertyanimation objectanimator
原文地址:http://blog.csdn.net/zhjali123/article/details/47041677