我开始用android学习动画,阅读https://developer.android.com/guide/topics/resources/animation-resource.html。
发现xml和ValueAnimator中有两个元素
前者用于动画对象的属性,但与链接页面提供的定义相混淆。即:
“在指定的时间内执行动画。表示ValueAnimator”
这两个标记具有相同的属性:
<objectAnimator
android:propertyName="string"
android:duration="int"
android:valueFrom="float | int | color"
android:valueTo="float | int | color"
android:startOffset="int"
android:repeatCount="int"
android:repeatMode=["repeat" | "reverse"]
android:valueType=["intType" | "floatType"]/>
<animator
android:duration="int"
android:valueFrom="float | int | color"
android:valueTo="float | int | color"
android:startOffset="int"
android:repeatCount="int"
android:repeatMode=["repeat" | "reverse"]
android:valueType=["intType" | "floatType"]/>有谁能解释一下不同之处,以及何时使用什么?如有任何答复和评论,我们将不胜感激。
发布于 2017-05-11 21:43:41
ObjectAnimator是ValueAnimator的子类。主要的区别是,在ValueAnimator情况下,您必须重写onAnimationUpdate(.)方法指定应用动画值的位置:
ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
view.setAlpha((Float) animation.getAnimatedValue());
}
});
animator.start();ObjectAnimator将自己处理这一问题:
ObjectAnimator.ofFloat(view, View.ALPHA, 0, 1).start();如果是XML,请注意objectAnimator的“objectAnimator”,它不存在于动画标记中。
另外,从API 23开始,您还可以同时动画几个属性:
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:repeatCount="1"
android:repeatMode="reverse">
<propertyValuesHolder android:propertyName="x" android:valueTo="400"/>
<propertyValuesHolder android:propertyName="y" android:valueTo="200"/>
</objectAnimator>和/或自定义动画帧:
<animator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:repeatCount="1"
android:repeatMode="reverse">
<propertyValuesHolder>
<keyframe android:fraction="0" android:value="1"/>
<keyframe android:fraction=".2" android:value=".4"/>
<keyframe android:fraction="1" android:value="0"/>
</propertyValuesHolder>
</animator>https://stackoverflow.com/questions/43260975
复制相似问题