安卓(Android) 怎么从右动画添加幻灯片(slide)

2019年10月14日21:59:50安卓(Android) 怎么从右动画添加幻灯片(slide)已关闭评论 538 2141字阅读7分8秒

1.简介

安卓(Android) 怎么从右动画添加幻灯片(slide)?——请看下文。

2.示例代码

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

import java.util.List;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
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 addSlideInFromRightAnimators(List<Animator> animators, final View view, int startTranslation,
      int animationDelay) {
    addXTranslationAnimators(animators, view, startTranslation, 0, animationDelay, null);
    addFadeAnimators(animators, view, 0, 1.0f, animationDelay);
  }

  
  private static void addXTranslationAnimators(List<Animator> animators, final View view, int startTranslation,
      final int endTranslation, int animationDelay, AnimatorListener listener) {
    
    
    view.setTranslationX(startTranslation);
    final ObjectAnimator translateAnimatorX = ObjectAnimator.ofFloat(view, View.TRANSLATION_X, startTranslation,
        endTranslation);
    translateAnimatorX.setInterpolator(sDecelerateQuintInterpolator);
    translateAnimatorX.setDuration(sAnimationDuration);
    translateAnimatorX.setStartDelay(animationDelay);
    if (listener != null) {
      translateAnimatorX.addListener(listener);
    }

    animators.add(translateAnimatorX);
  }

  
  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 );
  }
}

 
继续阅读
编程十万个怎么办