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