1 /*
2  * Copyright (C) 2018 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 
17 package com.android.launcher3.anim;
18 
19 import android.animation.Animator;
20 import android.animation.ValueAnimator;
21 import android.animation.ValueAnimator.AnimatorUpdateListener;
22 import android.view.View;
23 import android.view.ViewGroup;
24 
25 /**
26  * A convenience class to update a view's visibility state after an alpha animation.
27  */
28 public class AlphaUpdateListener extends AnimationSuccessListener
29         implements AnimatorUpdateListener {
30     public static final float ALPHA_CUTOFF_THRESHOLD = 0.01f;
31 
32     private View mView;
33 
AlphaUpdateListener(View v)34     public AlphaUpdateListener(View v) {
35         mView = v;
36     }
37 
38     @Override
onAnimationUpdate(ValueAnimator arg0)39     public void onAnimationUpdate(ValueAnimator arg0) {
40         updateVisibility(mView);
41     }
42 
43     @Override
onAnimationSuccess(Animator animator)44     public void onAnimationSuccess(Animator animator) {
45         updateVisibility(mView);
46     }
47 
48     @Override
onAnimationStart(Animator arg0)49     public void onAnimationStart(Animator arg0) {
50         // We want the views to be visible for animation, so fade-in/out is visible
51         mView.setVisibility(View.VISIBLE);
52     }
53 
updateVisibility(View view)54     public static void updateVisibility(View view) {
55         if (view.getAlpha() < ALPHA_CUTOFF_THRESHOLD && view.getVisibility() != View.INVISIBLE) {
56             view.setVisibility(View.INVISIBLE);
57         } else if (view.getAlpha() > ALPHA_CUTOFF_THRESHOLD
58                 && view.getVisibility() != View.VISIBLE) {
59             if (view instanceof ViewGroup) {
60                 ViewGroup viewGroup = ((ViewGroup) view);
61                 int oldFocusability = viewGroup.getDescendantFocusability();
62                 viewGroup.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
63                 viewGroup.setVisibility(View.VISIBLE);
64                 viewGroup.setDescendantFocusability(oldFocusability);
65             } else {
66                 view.setVisibility(View.VISIBLE);
67             }
68         }
69     }
70 }