[Android] Attribute animation (basic usage)

Transparency is represented by 0-1. 0 means fully transparent, 1 means opaque

  • Example : In 1s, change imageViewthe transparency from 1to 0.
//透明度起始为1,结束时为0
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0f);
animator.setDuration(1000);//时间1s
animator.start();

ofFloatParameters 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 ObjectAnimatorthe provided one setRepeatCount(int count). countFor the number of repetitions, -1it 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 AnimatorSetto combine these animations.
AnimatorSet: This class provides a play() method, which will return an AnimatorSet.Builderinstance after calling, AnimatorSet.Builderincluding the following four methods:

  • after(Animator anim): executes after inserting an existing animation into the incoming animation
  • after(long delay): delays the existing animation after the specified milliseconds
  • before(Animator anim): Executes before inserting an existing animation into the incoming animation
  • with(Animator anim): Execute the existing animation and the incoming animation at the same time

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325162615&siteId=291194637