如果要多个动画配合工作,需要把他们放入AnimatorSet中
ObjectAnimator animator1 = ObjectAnimator.ofFloat(...); animator1.setInterpolator(new LinearInterpolator()); ObjectAnimator animator2 = ObjectAnimator.ofInt(...); animator2.setInterpolator(new DecelerateInterpolator()); AnimatorSet animatorSet = new AnimatorSet(); // 两个动画依次执行 animatorSet.playSequentially(animator1, animator2); animatorSet.start();
使用 playSequentially()
,就可以让两个动画依次播放,而不用为它们设置监听器来手动为他们监管协作。
// 两个动画同时执行 animatorSet.playTogether(animator1, animator2); animatorSet.start();
使用playTogether(),就可以让两个动画同时播放
// 使用 AnimatorSet.play(animatorA).with/before/after(animatorB) // 的方式来精确配置各个 Animator 之间的关系 animatorSet.play(animator1).with(animator2); animatorSet.play(animator1).before(animator2); animatorSet.play(animator1).after(animator2); animatorSet.start();
还可以用play with/before/after这种形式
PropertyValuesHolders.ofKeyframe() 拆分同一个属性
除了合并多个属性和调配多个动画,你还可以在 PropertyValuesHolder
的基础上更进一步,通过设置 Keyframe
(关键帧),把同一个动画属性拆分成多个阶段。例如,你可以让一个进度增加到 100% 后再「反弹」回来。
// 在 0% 处开始 Keyframe keyframe1 = Keyframe.ofFloat(0, 0); // 时间经过 50% 的时候,动画完成度 100% Keyframe keyframe2 = Keyframe.ofFloat(0.5f, 100); // 时间经过 100% 的时候,动画完成度倒退到 80%,即反弹 20% Keyframe keyframe3 = Keyframe.ofFloat(1, 80); PropertyValuesHolder holder = PropertyValuesHolder.ofKeyframe("progress", keyframe1, keyframe2, keyframe3); ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, holder); animator.start();