安卓(Android) 怎么添加动画(Animation)以将视图(View)从指定的开始Alpha值淡入到结束值

2019年10月15日20:46:40安卓(Android) 怎么添加动画(Animation)以将视图(View)从指定的开始Alpha值淡入到结束值已关闭评论 425 1275字阅读4分15秒

1.简介

安卓(Android) 怎么添加动画(Animation)以将视图(View)从指定的开始Alpha值淡入到结束值?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

import java.util.List;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.view.View;
import android.view.animation.Interpolator;

public class Main {
  private static Interpolator sDecelerateQuintInterpolator;
  
  private static int sAnimationDuration = 450;

  
  public static void addFadeAnimators(List<Animator> animators, final View view, float startAlpha, final float endAlpha,
      int animationDelay) {
    if (startAlpha == endAlpha) {
      return;
    }

    view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    view.setAlpha(startAlpha);

    final ObjectAnimator fadeAnimator = ObjectAnimator.ofFloat(view, View.ALPHA, view.getAlpha(), endAlpha);
    fadeAnimator.setInterpolator(sDecelerateQuintInterpolator);
    fadeAnimator.setDuration(sAnimationDuration);
    fadeAnimator.setStartDelay(animationDelay);
    fadeAnimator.addListener(new AnimatorListenerAdapter() {
      @Override
      public void onAnimationEnd(Animator animation) {
        view.setAlpha(endAlpha);
        view.setLayerType(View.LAYER_TYPE_NONE, null);
      }
    });
    animators.add(fadeAnimator);
  }

  
  public static void addFadeAnimators(List<Animator> animators, final View view, float startAlpha,
      final float endAlpha) {
    addFadeAnimators(animators, view, startAlpha, endAlpha, 0 );
  }
}

 
继续阅读