blob: e4858c4d0a11a81d2a5c9fddf59cf7905a00d3bf [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 Haasefaebd8f2012-05-18 14:17:57 -070017package android.view.transition;
18
19import android.animation.Animator;
20import android.animation.ArgbEvaluator;
21import android.animation.ObjectAnimator;
22import android.graphics.drawable.ColorDrawable;
23import android.graphics.drawable.Drawable;
Chet Haase08735182013-06-04 10:44:40 -070024import android.util.ArrayMap;
Chet Haasefaebd8f2012-05-18 14:17:57 -070025import android.view.View;
26import android.view.ViewGroup;
27import android.widget.TextView;
28
Chet Haase08735182013-06-04 10:44:40 -070029import java.util.Map;
Chet Haasefaebd8f2012-05-18 14:17:57 -070030
31/**
32 * This transition tracks changes during scene changes to the
33 * {@link View#setBackground(android.graphics.drawable.Drawable) background}
34 * property of its target views (when the background is a
35 * {@link ColorDrawable}, as well as the
36 * {@link TextView#setTextColor(android.content.res.ColorStateList)
37 * color} of the text for target TextViews. If the color changes between
38 * scenes, the color change is animated.
39 */
40public class Recolor extends Transition {
41
42 private static final String PROPNAME_BACKGROUND = "android:recolor:background";
43 private static final String PROPNAME_TEXT_COLOR = "android:recolor:textColor";
44
45 @Override
46 protected void captureValues(TransitionValues values, boolean start) {
47 values.values.put(PROPNAME_BACKGROUND, values.view.getBackground());
48 if (values.view instanceof TextView) {
49 values.values.put(PROPNAME_TEXT_COLOR, ((TextView)values.view).getCurrentTextColor());
50 }
51 }
52
53 @Override
Chet Haase2ea7f8b2013-06-21 15:00:05 -070054 protected Animator play(ViewGroup sceneRoot, TransitionValues startValues,
Chet Haasefaebd8f2012-05-18 14:17:57 -070055 TransitionValues endValues) {
56 if (startValues == null || endValues == null) {
Chet Haase2ea7f8b2013-06-21 15:00:05 -070057 return null;
Chet Haasefaebd8f2012-05-18 14:17:57 -070058 }
59 final View view = endValues.view;
60 Drawable startBackground = (Drawable) startValues.values.get(PROPNAME_BACKGROUND);
61 Drawable endBackground = (Drawable) endValues.values.get(PROPNAME_BACKGROUND);
62 boolean changed = false;
63 if (startBackground instanceof ColorDrawable && endBackground instanceof ColorDrawable) {
64 ColorDrawable startColor = (ColorDrawable) startBackground;
65 ColorDrawable endColor = (ColorDrawable) endBackground;
66 if (startColor.getColor() != endColor.getColor()) {
67 endColor.setColor(startColor.getColor());
68 changed = true;
Chet Haase2ea7f8b2013-06-21 15:00:05 -070069 return ObjectAnimator.ofObject(endBackground, "color",
70 new ArgbEvaluator(), startColor.getColor(), endColor.getColor());
Chet Haasefaebd8f2012-05-18 14:17:57 -070071 }
72 }
73 if (view instanceof TextView) {
74 TextView textView = (TextView) view;
75 int start = (Integer) startValues.values.get(PROPNAME_TEXT_COLOR);
76 int end = (Integer) endValues.values.get(PROPNAME_TEXT_COLOR);
77 if (start != end) {
78 textView.setTextColor(end);
79 changed = true;
Chet Haase2ea7f8b2013-06-21 15:00:05 -070080 return ObjectAnimator.ofObject(textView, "textColor",
Chet Haasefaebd8f2012-05-18 14:17:57 -070081 new ArgbEvaluator(), start, end);
Chet Haasefaebd8f2012-05-18 14:17:57 -070082 }
83 }
Chet Haase2ea7f8b2013-06-21 15:00:05 -070084 return null;
Chet Haasefaebd8f2012-05-18 14:17:57 -070085 }
86}