• 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 
17 package com.android.car.apps.common.util;
18 
19 import android.animation.Animator;
20 import android.animation.AnimatorListenerAdapter;
21 import android.annotation.NonNull;
22 import android.view.View;
23 import android.widget.TextView;
24 
25 import androidx.annotation.Nullable;
26 import androidx.annotation.StringRes;
27 
28 /**
29  * Utility methods to operate over views.
30  */
31 public class ViewUtils {
32     /**
33      * Hides a view using a fade-out animation
34      *
35      * @param view     {@link View} to be hidden
36      * @param duration animation duration in milliseconds.
37      */
hideViewAnimated(@onNull View view, int duration)38     public static void hideViewAnimated(@NonNull View view, int duration) {
39         // Cancel existing animation to avoid race condition
40         // if show and hide are called at the same time
41         view.animate().cancel();
42 
43         if (!view.isLaidOut()) {
44             // If the view hasn't been displayed yet, just adjust visibility without animation
45             view.setVisibility(View.GONE);
46             return;
47         }
48 
49         view.animate()
50                 .setDuration(duration)
51                 .setListener(hideViewAfterAnimation(view))
52                 .alpha(0f);
53     }
54 
55     /** Returns an AnimatorListener that hides the view at the end. */
hideViewAfterAnimation(View view)56     public static Animator.AnimatorListener hideViewAfterAnimation(View view) {
57         return new AnimatorListenerAdapter() {
58             @Override
59             public void onAnimationEnd(Animator animation) {
60                 view.setVisibility(View.GONE);
61             }
62         };
63     }
64 
65     /**
66      * Shows a view using a fade-in animation
67      *
68      * @param view     {@link View} to be shown
69      * @param duration animation duration in milliseconds.
70      */
71     public static void showViewAnimated(@NonNull View view, int duration) {
72         // Cancel existing animation to avoid race condition
73         // if show and hide are called at the same time
74         view.animate().cancel();
75 
76         // Do the animation even if the view isn't laid out which is often the case for a view
77         // that isn't shown (otherwise the view just pops onto the screen...
78 
79         view.animate()
80                 .setDuration(duration)
81                 .setListener(new AnimatorListenerAdapter() {
82                     @Override
83                     public void onAnimationStart(Animator animation) {
84                         view.setVisibility(View.VISIBLE);
85                     }
86                 })
87                 .alpha(1f);
88     }
89 
90     /** Sets the visibility of the (optional) view to {@link View#VISIBLE} or {@link View#GONE}. */
91     public static void setVisible(@Nullable View view, boolean visible) {
92         if (view != null) {
93             view.setVisibility(visible ? View.VISIBLE : View.GONE);
94         }
95     }
96 
97     /**
98      * Sets the visibility of the (optional) view to {@link View#INVISIBLE} or {@link View#VISIBLE}.
99      */
100     public static void setInvisible(@Nullable View view, boolean invisible) {
101         if (view != null) {
102             view.setVisibility(invisible ? View.INVISIBLE : View.VISIBLE);
103         }
104     }
105 
106     /** Sets the text to the (optional) {@link TextView}. */
107     public static void setText(@Nullable TextView view, @StringRes int resId) {
108         if (view != null) {
109             view.setText(resId);
110         }
111     }
112 
113     /** Sets the text to the (optional) {@link TextView}. */
114     public static void setText(@Nullable TextView view, CharSequence text) {
115         if (view != null) {
116             view.setText(text);
117         }
118     }
119 }
120