Record the implementation of several animations in Android

1. View animation

View animation only produces animation effects on the view, and the properties of the View (position, size, angle, etc.) do not actually change. Android provides four APIs, AlphaAnimation, RotateAnimation, TranslateAnimation, and ScaleAnimation, to achieve view animation effects. At the same time, AnimationSet is provided to implement animation collections, so as to mix and use multiple animations. The following example uses the AlphaAnimation animation effect on a View named mTestView, and other animations are used in the same way.
1. AlphaAnimation example:

AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
alphaAnimation.setDuration(2000);
mTestView.startAnimation(alphaAnimation);
//添加动画监听回调
alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });

2. Use AnimationSet to mix multiple animation effects

 AnimationSet animationSet = new AnimationSet(true);
 AlphaAnimation alphaAnimation1 = new AlphaAnimation(0, 1);
 alphaAnimation1.setDuration(2000);
 animationSet.addAnimation(alphaAnimation1);
 TranslateAnimation translateAnimation1 = new TranslateAnimation(0, 200, 0, 200);
 translateAnimation1.setDuration(2000);
 animationSet.addAnimation(translateAnimation1);
 mTestView.startAnimation(animationSet);

2. Property animation

Property animation is an animation framework added after Android 3.0. The most commonly used is the combination of AnimatorSet and ObjectAnimator. ObjectAnimator can finely control an attribute value of an object, and change the corresponding attribute of the animation object while realizing the animation effect. AnimatorSet can combine multiple ObjectAnimators to achieve multi-animation mixing.
1. Use of ObjectAnimator

//参数一:要操纵的view;
//参数二:要操纵的属性(translationX、translationY、rotationX、rotationY、rotation、scaleX、scaleY)
//参数三:可变数组参数,代表属性变化的一个取值过程。
 ObjectAnimator translationX = ObjectAnimator.ofFloat(mTestView, "translationX", 300);
 translationX.setDuration(500);
 translationX.start();

2. PropertyValuesHolder: apply multiple animations to multiple properties of the same object at the same time
Example : zoom in and out during panning

 PropertyValuesHolder pvh1 = PropertyValuesHolder.ofFloat("translationX", 300f);
 PropertyValuesHolder pvh2 = PropertyValuesHolder.ofFloat("scaleX", 1f,1.5f);
 PropertyValuesHolder pvh3 = PropertyValuesHolder.ofFloat("scaleY", 1f, 1.5f);
 ObjectAnimator.ofPropertyValuesHolder(mTestView, pvh1, pvh2, pvh3).setDuration(2000).start();

3. AnimatorSet: Combine multiple animations, and can control the precise sequence of multiple animations.
Example: Execute 3 animations in sequence: first pan mTestView, then mBtnAnimatorSet scales horizontally, and finally mBtnAnimatorSet scales vertically

 ObjectAnimator translationX1 = ObjectAnimator.ofFloat(mTestView, "translationX", 300);
ObjectAnimator scaleX1 = ObjectAnimator.ofFloat(mBtnAnimatorSet, "scaleX", 1.5f);
ObjectAnimator scaleY1 = ObjectAnimator.ofFloat(mBtnAnimatorSet, "scaleY", 1.5f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration(2000);
animatorSet.playSequentially(translationX1, scaleX1, scaleY1);
animatorSet.start();

4. Use View's animate method to implement property animation
After Android 3.0, View added the animate method to directly implement property animation.

mTestView.animate()
         .scaleX(1.5f)
         .x(300)
         .setDuration(1000)
         .withStartAction(new Runnable() {
              @Override
              public void run() {

              }
          })
         .withEndAction(new Runnable() {
              @Override
              public void run() {

              }
         }).start();

5. Layout animation
Layout animation acts on the layout. When a view is added to the layout, an animation transition effect will be added. For example: add an AlphaAnimation to a RelativeLayout, and the view in the layout will appear with the animation effect of AlphaAnimation.

 AlphaAnimation aa = new AlphaAnimation(0, 1);
 aa.setDuration(3000);
 LayoutAnimationController lac = new LayoutAnimationController(aa, 1f);
//LayoutAnimationController.ORDER_NORMAL:布局中的view按顺序出现
//LayoutAnimationController.ORDER_RANDOM:布局中的view按随机顺序出现
//LayoutAnimationController.ORDER_REVERSE:布局中的view反序出现
 lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
 mTestLayout.setLayoutAnimation(lac);

Guess you like

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