Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 1 | /* |
| 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 Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 16 | |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 17 | package android.view.transition; |
| 18 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.ArgbEvaluator; |
| 21 | import android.animation.ObjectAnimator; |
| 22 | import android.graphics.drawable.ColorDrawable; |
| 23 | import android.graphics.drawable.Drawable; |
Chet Haase | 0873518 | 2013-06-04 10:44:40 -0700 | [diff] [blame] | 24 | import android.util.ArrayMap; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 25 | import android.view.View; |
| 26 | import android.view.ViewGroup; |
| 27 | import android.widget.TextView; |
| 28 | |
Chet Haase | 0873518 | 2013-06-04 10:44:40 -0700 | [diff] [blame] | 29 | import java.util.Map; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 30 | |
| 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 | */ |
| 40 | public 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 Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 54 | protected Animator play(ViewGroup sceneRoot, TransitionValues startValues, |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 55 | TransitionValues endValues) { |
| 56 | if (startValues == null || endValues == null) { |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 57 | return null; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 58 | } |
| 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 Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 69 | return ObjectAnimator.ofObject(endBackground, "color", |
| 70 | new ArgbEvaluator(), startColor.getColor(), endColor.getColor()); |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 71 | } |
| 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 Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 80 | return ObjectAnimator.ofObject(textView, "textColor", |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 81 | new ArgbEvaluator(), start, end); |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 84 | return null; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 85 | } |
| 86 | } |