• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.managedprovisioning.common;
17 
18 import android.animation.Animator;
19 import android.animation.AnimatorListenerAdapter;
20 import android.animation.AnimatorSet;
21 import android.animation.ValueAnimator;
22 import android.animation.ValueAnimator.AnimatorUpdateListener;
23 import android.annotation.TargetApi;
24 import android.view.View;
25 import java.util.ArrayList;
26 import java.util.List;
27 
28 /**
29  * Performs fade out-fade in animations on a {@link View} list.
30  */
31 public class CrossFadeHelper {
32     public interface Callback {
fadeOutCompleted()33         void fadeOutCompleted();
fadeInCompleted()34         void fadeInCompleted();
35     }
36 
37     private final List<View> mViews;
38     private final int mDuration;
39     private final Callback mCallback;
40     private final ValueAnimator mFadeOutAnimator;
41     private final ValueAnimator mFadeInAnimator;
42     private final AnimatorSet mAnimatorSet;
43 
44     private final AnimatorUpdateListener mAnimatorUpdateListener = new AnimatorUpdateListener() {
45         @Override
46         public void onAnimationUpdate(ValueAnimator animation) {
47             final float alpha = (float) animation.getAnimatedValue();
48             for (View view : mViews) {
49                 view.setAlpha(alpha);
50             }
51         }
52     };
53 
CrossFadeHelper(List<View> viewsToAnimate, int duration, Callback callback)54     public CrossFadeHelper(List<View> viewsToAnimate, int duration, Callback callback) {
55         mViews = new ArrayList<>(viewsToAnimate);
56         mDuration = duration;
57         mCallback = callback;
58         mFadeOutAnimator = getFadeOutAnimator();
59         mFadeInAnimator = getFadeInAnimator();
60         mAnimatorSet = new AnimatorSet();
61         mAnimatorSet.playSequentially(mFadeOutAnimator, mFadeInAnimator);
62     }
63 
start()64     public void start() {
65         mAnimatorSet.start();
66     }
67 
getFadeInAnimator()68     private ValueAnimator getFadeInAnimator() {
69         final ValueAnimator fadeInAnimator = ValueAnimator.ofFloat(0f, 1f);
70         fadeInAnimator.addUpdateListener(mAnimatorUpdateListener);
71         fadeInAnimator.setDuration(mDuration);
72         fadeInAnimator.addListener(new AnimatorListenerAdapter() {
73             @Override
74             public void onAnimationEnd(Animator animation) {
75                 if (mCallback != null) {
76                     mCallback.fadeInCompleted();
77                 }
78             }
79         });
80         return fadeInAnimator;
81     }
82 
getFadeOutAnimator()83     private ValueAnimator getFadeOutAnimator() {
84         final ValueAnimator fadeOutAnimator = ValueAnimator.ofFloat(1f, 0f);
85         fadeOutAnimator.addUpdateListener(mAnimatorUpdateListener);
86         fadeOutAnimator.setDuration(mDuration);
87         fadeOutAnimator.addListener(new AnimatorListenerAdapter() {
88             @Override
89             public void onAnimationEnd(Animator animation) {
90                 if (mCallback != null) {
91                     mCallback.fadeOutCompleted();
92                 }
93             }
94         });
95         return fadeOutAnimator;
96     }
97 
cleanup()98     public void cleanup() {
99         mFadeInAnimator.removeAllUpdateListeners();
100         mFadeInAnimator.removeAllListeners();
101         mFadeOutAnimator.removeAllUpdateListeners();
102         mFadeOutAnimator.removeAllListeners();
103         mAnimatorSet.cancel();
104         mViews.clear();
105     }
106 }
107