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