blob: 1638f670941233550ea96dd9c20b516e5ae95ece [file] [log] [blame]
Chet Haasefaebd8f2012-05-18 14:17:57 -07001/*
2 * Copyright (C) 2013 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 */
Chet Haase6ebe3de2013-06-17 16:50:50 -070016
Chet Haased82c8ac2013-08-26 14:20:16 -070017package android.transition;
Chet Haasefaebd8f2012-05-18 14:17:57 -070018
19import android.animation.Animator;
Chet Haasefaebd8f2012-05-18 14:17:57 -070020import android.animation.ObjectAnimator;
21import android.graphics.drawable.ColorDrawable;
22import android.graphics.drawable.Drawable;
23import android.view.View;
24import android.view.ViewGroup;
25import android.widget.TextView;
26
Chet Haasefaebd8f2012-05-18 14:17:57 -070027/**
28 * This transition tracks changes during scene changes to the
29 * {@link View#setBackground(android.graphics.drawable.Drawable) background}
30 * property of its target views (when the background is a
31 * {@link ColorDrawable}, as well as the
32 * {@link TextView#setTextColor(android.content.res.ColorStateList)
33 * color} of the text for target TextViews. If the color changes between
34 * scenes, the color change is animated.
Chet Haased82c8ac2013-08-26 14:20:16 -070035 *
36 * @hide
Chet Haasefaebd8f2012-05-18 14:17:57 -070037 */
38public class Recolor extends Transition {
39
40 private static final String PROPNAME_BACKGROUND = "android:recolor:background";
41 private static final String PROPNAME_TEXT_COLOR = "android:recolor:textColor";
42
Chet Haased82c8ac2013-08-26 14:20:16 -070043 private void captureValues(TransitionValues transitionValues) {
44 transitionValues.values.put(PROPNAME_BACKGROUND, transitionValues.view.getBackground());
45 if (transitionValues.view instanceof TextView) {
46 transitionValues.values.put(PROPNAME_TEXT_COLOR,
47 ((TextView)transitionValues.view).getCurrentTextColor());
Chet Haasefaebd8f2012-05-18 14:17:57 -070048 }
49 }
50
51 @Override
Chet Haased82c8ac2013-08-26 14:20:16 -070052 public void captureStartValues(TransitionValues transitionValues) {
53 captureValues(transitionValues);
54 }
55
56 @Override
57 public void captureEndValues(TransitionValues transitionValues) {
58 captureValues(transitionValues);
59 }
60
61 @Override
62 public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues,
Chet Haasefaebd8f2012-05-18 14:17:57 -070063 TransitionValues endValues) {
64 if (startValues == null || endValues == null) {
Chet Haase2ea7f8b2013-06-21 15:00:05 -070065 return null;
Chet Haasefaebd8f2012-05-18 14:17:57 -070066 }
67 final View view = endValues.view;
68 Drawable startBackground = (Drawable) startValues.values.get(PROPNAME_BACKGROUND);
69 Drawable endBackground = (Drawable) endValues.values.get(PROPNAME_BACKGROUND);
70 boolean changed = false;
71 if (startBackground instanceof ColorDrawable && endBackground instanceof ColorDrawable) {
72 ColorDrawable startColor = (ColorDrawable) startBackground;
73 ColorDrawable endColor = (ColorDrawable) endBackground;
74 if (startColor.getColor() != endColor.getColor()) {
75 endColor.setColor(startColor.getColor());
76 changed = true;
George Mount1ffb2802013-10-09 16:13:54 -070077 return ObjectAnimator.ofArgb(endBackground, "color", startColor.getColor(),
78 endColor.getColor());
Chet Haasefaebd8f2012-05-18 14:17:57 -070079 }
80 }
81 if (view instanceof TextView) {
82 TextView textView = (TextView) view;
83 int start = (Integer) startValues.values.get(PROPNAME_TEXT_COLOR);
84 int end = (Integer) endValues.values.get(PROPNAME_TEXT_COLOR);
85 if (start != end) {
86 textView.setTextColor(end);
87 changed = true;
George Mount1ffb2802013-10-09 16:13:54 -070088 return ObjectAnimator.ofArgb(textView, "textColor", start, end);
Chet Haasefaebd8f2012-05-18 14:17:57 -070089 }
90 }
Chet Haase2ea7f8b2013-06-21 15:00:05 -070091 return null;
Chet Haasefaebd8f2012-05-18 14:17:57 -070092 }
93}