1 package com.android.car.media.widgets; 2 3 import android.animation.Animator; 4 import android.animation.AnimatorListenerAdapter; 5 import android.annotation.NonNull; 6 import android.view.View; 7 8 /** 9 * Utility methods to operate over views. 10 */ 11 public class ViewUtils { 12 /** 13 * Hides a view using a fade-out animation 14 * 15 * @param view {@link View} to be hidden 16 * @param duration animation duration in milliseconds. 17 */ 18 public static void hideViewAnimated(@NonNull View view, int duration) { 19 if (view.getVisibility() == View.GONE) { 20 return; 21 } 22 if (!view.isLaidOut()) { 23 // If the view hasn't been displayed yet, just adjust visibility without animation 24 view.setVisibility(View.GONE); 25 return; 26 } 27 view.animate() 28 .alpha(0f) 29 .setDuration(duration) 30 .setListener(new AnimatorListenerAdapter() { 31 @Override 32 public void onAnimationEnd(Animator animation) { 33 view.setVisibility(View.GONE); 34 } 35 }); 36 } 37 38 /** 39 * Shows a view using a fade-in animation 40 * 41 * @param view {@link View} to be shown 42 * @param duration animation duration in milliseconds. 43 */ 44 public static void showViewAnimated(@NonNull View view, int duration) { 45 if (view.getVisibility() == View.VISIBLE) { 46 return; 47 } 48 if (!view.isLaidOut()) { 49 // If the view hasn't been displayed yet, just adjust visibility without animation 50 view.setVisibility(View.VISIBLE); 51 return; 52 } 53 view.animate() 54 .alpha(1f) 55 .setDuration(duration) 56 .setListener(new AnimatorListenerAdapter() { 57 @Override 58 public void onAnimationStart(Animator animation) { 59 view.setAlpha(0f); 60 view.setVisibility(View.VISIBLE); 61 } 62 }); 63 } 64 } 65