blob: 70111d12fdee4d00e0987a8807662dd7c6a3a0cf [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;
20import android.animation.ArgbEvaluator;
21import android.animation.ObjectAnimator;
22import android.graphics.drawable.ColorDrawable;
23import android.graphics.drawable.Drawable;
24import android.view.View;
25import android.view.ViewGroup;
26import android.widget.TextView;
27
Chet Haasefaebd8f2012-05-18 14:17:57 -070028/**
29 * This transition tracks changes during scene changes to the
30 * {@link View#setBackground(android.graphics.drawable.Drawable) background}
31 * property of its target views (when the background is a
32 * {@link ColorDrawable}, as well as the
33 * {@link TextView#setTextColor(android.content.res.ColorStateList)
34 * color} of the text for target TextViews. If the color changes between
35 * scenes, the color change is animated.
Chet Haased82c8ac2013-08-26 14:20:16 -070036 *
37 * @hide
Chet Haasefaebd8f2012-05-18 14:17:57 -070038 */
39public class Recolor extends Transition {
40
41 private static final String PROPNAME_BACKGROUND = "android:recolor:background";
42 private static final String PROPNAME_TEXT_COLOR = "android:recolor:textColor";
43
Chet Haased82c8ac2013-08-26 14:20:16 -070044 private void captureValues(TransitionValues transitionValues) {
45 transitionValues.values.put(PROPNAME_BACKGROUND, transitionValues.view.getBackground());
46 if (transitionValues.view instanceof TextView) {
47 transitionValues.values.put(PROPNAME_TEXT_COLOR,
48 ((TextView)transitionValues.view).getCurrentTextColor());
Chet Haasefaebd8f2012-05-18 14:17:57 -070049 }
50 }
51
52 @Override
Chet Haased82c8ac2013-08-26 14:20:16 -070053 public void captureStartValues(TransitionValues transitionValues) {
54 captureValues(transitionValues);
55 }
56
57 @Override
58 public void captureEndValues(TransitionValues transitionValues) {
59 captureValues(transitionValues);
60 }
61
62 @Override
63 public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues,
Chet Haasefaebd8f2012-05-18 14:17:57 -070064 TransitionValues endValues) {
65 if (startValues == null || endValues == null) {
Chet Haase2ea7f8b2013-06-21 15:00:05 -070066 return null;
Chet Haasefaebd8f2012-05-18 14:17:57 -070067 }
68 final View view = endValues.view;
69 Drawable startBackground = (Drawable) startValues.values.get(PROPNAME_BACKGROUND);
70 Drawable endBackground = (Drawable) endValues.values.get(PROPNAME_BACKGROUND);
71 boolean changed = false;
72 if (startBackground instanceof ColorDrawable && endBackground instanceof ColorDrawable) {
73 ColorDrawable startColor = (ColorDrawable) startBackground;
74 ColorDrawable endColor = (ColorDrawable) endBackground;
75 if (startColor.getColor() != endColor.getColor()) {
76 endColor.setColor(startColor.getColor());
77 changed = true;
Chet Haase2ea7f8b2013-06-21 15:00:05 -070078 return ObjectAnimator.ofObject(endBackground, "color",
79 new ArgbEvaluator(), startColor.getColor(), endColor.getColor());
Chet Haasefaebd8f2012-05-18 14:17:57 -070080 }
81 }
82 if (view instanceof TextView) {
83 TextView textView = (TextView) view;
84 int start = (Integer) startValues.values.get(PROPNAME_TEXT_COLOR);
85 int end = (Integer) endValues.values.get(PROPNAME_TEXT_COLOR);
86 if (start != end) {
87 textView.setTextColor(end);
88 changed = true;
Chet Haase2ea7f8b2013-06-21 15:00:05 -070089 return ObjectAnimator.ofObject(textView, "textColor",
Chet Haasefaebd8f2012-05-18 14:17:57 -070090 new ArgbEvaluator(), start, end);
Chet Haasefaebd8f2012-05-18 14:17:57 -070091 }
92 }
Chet Haase2ea7f8b2013-06-21 15:00:05 -070093 return null;
Chet Haasefaebd8f2012-05-18 14:17:57 -070094 }
95}