1 /* 2 * Copyright (C) 2016 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.dvr.ui; 18 19 import android.animation.Animator; 20 import android.animation.ObjectAnimator; 21 import android.content.Context; 22 import android.content.res.TypedArray; 23 import android.graphics.Color; 24 import android.graphics.drawable.ColorDrawable; 25 import android.graphics.drawable.Drawable; 26 import android.transition.Transition; 27 import android.transition.TransitionValues; 28 import android.transition.Visibility; 29 import android.util.AttributeSet; 30 import android.view.ViewGroup; 31 import com.android.tv.R; 32 33 /** This transition fades in/out of the background of the view by changing the background color. */ 34 public class FadeBackground extends Transition { 35 private final int mMode; 36 FadeBackground(Context context, AttributeSet attrs)37 public FadeBackground(Context context, AttributeSet attrs) { 38 super(context, attrs); 39 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FadeBackground); 40 mMode = a.getInt(R.styleable.FadeBackground_fadingMode, Visibility.MODE_IN); 41 a.recycle(); 42 } 43 44 @Override captureStartValues(TransitionValues transitionValues)45 public void captureStartValues(TransitionValues transitionValues) {} 46 47 @Override captureEndValues(TransitionValues transitionValues)48 public void captureEndValues(TransitionValues transitionValues) {} 49 50 @Override createAnimator( ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues)51 public Animator createAnimator( 52 ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues) { 53 if (startValues == null || endValues == null) { 54 return null; 55 } 56 Drawable background = endValues.view.getBackground(); 57 if (background instanceof ColorDrawable) { 58 int color = ((ColorDrawable) background).getColor(); 59 int transparentColor = 60 Color.argb(0, Color.red(color), Color.green(color), Color.blue(color)); 61 return mMode == Visibility.MODE_OUT 62 ? ObjectAnimator.ofArgb(background, "color", transparentColor) 63 : ObjectAnimator.ofArgb(background, "color", transparentColor, color); 64 } 65 return null; 66 } 67 } 68