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