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 | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 17 | package android.transition; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 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; |
| 24 | import android.view.View; |
| 25 | import android.view.ViewGroup; |
| 26 | import android.widget.TextView; |
| 27 | |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 28 | /** |
| 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 Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 36 | * |
| 37 | * @hide |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 38 | */ |
| 39 | public 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 Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 44 | 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 Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | @Override |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 53 | 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 Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 64 | TransitionValues endValues) { |
| 65 | if (startValues == null || endValues == null) { |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 66 | return null; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 67 | } |
| 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 Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 78 | return ObjectAnimator.ofObject(endBackground, "color", |
| 79 | new ArgbEvaluator(), startColor.getColor(), endColor.getColor()); |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 80 | } |
| 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 Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 89 | return ObjectAnimator.ofObject(textView, "textColor", |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 90 | new ArgbEvaluator(), start, end); |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 93 | return null; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 94 | } |
| 95 | } |