1 /* 2 * Copyright (C) 2015 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.tv.ui; 18 19 import android.animation.Animator; 20 import android.animation.ValueAnimator; 21 import android.os.Build; 22 import android.util.Log; 23 import android.view.View; 24 import android.view.ViewGroup.LayoutParams; 25 26 import java.lang.reflect.InvocationTargetException; 27 import java.lang.reflect.Method; 28 29 /** A class that includes convenience methods for view classes. */ 30 public class ViewUtils { 31 private static final String TAG = "ViewUtils"; 32 ViewUtils()33 private ViewUtils() { 34 // Prevent instantiation. 35 } 36 setTransitionAlpha(View v, float alpha)37 public static void setTransitionAlpha(View v, float alpha) { 38 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { 39 v.setTransitionAlpha(alpha); 40 } 41 Method method; 42 try { 43 method = View.class.getDeclaredMethod("setTransitionAlpha", Float.TYPE); 44 method.invoke(v, alpha); 45 } catch (NoSuchMethodException 46 | IllegalAccessException 47 | IllegalArgumentException 48 | InvocationTargetException e) { 49 Log.e(TAG, "Fail to call View.setTransitionAlpha", e); 50 } 51 } 52 53 /** 54 * Creates an animator in view's height 55 * 56 * @param target the {@link view} animator performs on. 57 */ createHeightAnimator( final View target, int initialHeight, int targetHeight)58 public static Animator createHeightAnimator( 59 final View target, int initialHeight, int targetHeight) { 60 ValueAnimator animator = ValueAnimator.ofInt(initialHeight, targetHeight); 61 animator.addUpdateListener( 62 new ValueAnimator.AnimatorUpdateListener() { 63 @Override 64 public void onAnimationUpdate(ValueAnimator animation) { 65 int value = (Integer) animation.getAnimatedValue(); 66 if (value == 0) { 67 if (target.getVisibility() != View.GONE) { 68 target.setVisibility(View.GONE); 69 } 70 } else { 71 if (target.getVisibility() != View.VISIBLE) { 72 target.setVisibility(View.VISIBLE); 73 } 74 setLayoutHeight(target, value); 75 } 76 } 77 }); 78 return animator; 79 } 80 81 /** Gets view's layout height. */ getLayoutHeight(View view)82 public static int getLayoutHeight(View view) { 83 LayoutParams layoutParams = view.getLayoutParams(); 84 return layoutParams.height; 85 } 86 87 /** Sets view's layout height. */ setLayoutHeight(View view, int height)88 public static void setLayoutHeight(View view, int height) { 89 LayoutParams layoutParams = view.getLayoutParams(); 90 if (height != layoutParams.height) { 91 layoutParams.height = height; 92 view.setLayoutParams(layoutParams); 93 } 94 } 95 } 96