Transparency is represented by 0-1. 0 means fully transparent, 1 means opaque
- Example : In 1s, change
imageView
the transparency from1
to0
.
//透明度起始为1,结束时为0
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0f);
animator.setDuration(1000);//时间1s
animator.start();
ofFloat
Parameters in: imageView
: Execute animation View
; "alpha"
: Indicates transparent animation; 1f
: Initial transparency; 0f
: Transparency after animation ends;
Two animations are performed here, and it can be seen that the 2s time is evenly distributed to these two animations.
If you want to perform three, four... animations, just add a few more parameters later. This also applies to several other animation effects: rotate, move, scale
If you want to keep it repeating , you can use
ObjectAnimator
the provided onesetRepeatCount(int count)
.count
For the number of repetitions,-1
it means repeating all the time.
animator.setRepeatCount(-1);
2.5 Combining animations
If only these basic animations can't meet our actual application, there is also a class AnimatorSet
to combine these animations. AnimatorSet
: This class provides a play() method, which will return an AnimatorSet.Builder
instance after calling, AnimatorSet.Builder
including the following four methods:
after(Animator anim)
: executes after inserting an existing animation into the incoming animationafter(long delay)
: delays the existing animation after the specified millisecondsbefore(Animator anim)
: Executes before inserting an existing animation into the incoming animationwith(Animator anim)
: Execute the existing animation and the incoming animation at the same time