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