| /* |
| * Copyright (C) 2013 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package android.view.transition; |
| |
| import android.animation.Animator; |
| import android.animation.ArgbEvaluator; |
| import android.animation.ObjectAnimator; |
| import android.graphics.drawable.ColorDrawable; |
| import android.graphics.drawable.Drawable; |
| import android.view.View; |
| import android.view.ViewGroup; |
| import android.widget.TextView; |
| |
| import java.util.HashMap; |
| |
| /** |
| * This transition tracks changes during scene changes to the |
| * {@link View#setBackground(android.graphics.drawable.Drawable) background} |
| * property of its target views (when the background is a |
| * {@link ColorDrawable}, as well as the |
| * {@link TextView#setTextColor(android.content.res.ColorStateList) |
| * color} of the text for target TextViews. If the color changes between |
| * scenes, the color change is animated. |
| */ |
| public class Recolor extends Transition { |
| |
| private static final String PROPNAME_BACKGROUND = "android:recolor:background"; |
| private static final String PROPNAME_TEXT_COLOR = "android:recolor:textColor"; |
| |
| @Override |
| protected void captureValues(TransitionValues values, boolean start) { |
| values.values.put(PROPNAME_BACKGROUND, values.view.getBackground()); |
| if (values.view instanceof TextView) { |
| values.values.put(PROPNAME_TEXT_COLOR, ((TextView)values.view).getCurrentTextColor()); |
| } |
| } |
| |
| @Override |
| protected boolean prePlay(ViewGroup sceneRoot, TransitionValues startValues, |
| TransitionValues endValues) { |
| if (startValues == null || endValues == null) { |
| return false; |
| } |
| final View view = endValues.view; |
| Drawable startBackground = (Drawable) startValues.values.get(PROPNAME_BACKGROUND); |
| Drawable endBackground = (Drawable) endValues.values.get(PROPNAME_BACKGROUND); |
| boolean changed = false; |
| if (startBackground instanceof ColorDrawable && endBackground instanceof ColorDrawable) { |
| ColorDrawable startColor = (ColorDrawable) startBackground; |
| ColorDrawable endColor = (ColorDrawable) endBackground; |
| if (startColor.getColor() != endColor.getColor()) { |
| endColor.setColor(startColor.getColor()); |
| changed = true; |
| } |
| } |
| if (view instanceof TextView) { |
| TextView textView = (TextView) view; |
| int start = (Integer) startValues.values.get(PROPNAME_TEXT_COLOR); |
| int end = (Integer) endValues.values.get(PROPNAME_TEXT_COLOR); |
| if (start != end) { |
| textView.setTextColor(end); |
| changed = true; |
| } |
| } |
| return changed; |
| } |
| |
| @Override |
| protected Animator play(ViewGroup sceneRoot, TransitionValues startValues, |
| TransitionValues endValues) { |
| if (startValues == null || endValues == null) { |
| return null; |
| } |
| ObjectAnimator anim = null; |
| final View view = endValues.view; |
| HashMap<String, Object> startVals = startValues.values; |
| HashMap<String, Object> endVals = endValues.values; |
| Drawable startBackground = (Drawable) startVals.get(PROPNAME_BACKGROUND); |
| Drawable endBackground = (Drawable) endVals.get(PROPNAME_BACKGROUND); |
| if (startBackground instanceof ColorDrawable && endBackground instanceof ColorDrawable) { |
| ColorDrawable startColor = (ColorDrawable) startBackground; |
| ColorDrawable endColor = (ColorDrawable) endBackground; |
| if (startColor.getColor() != endColor.getColor()) { |
| anim = ObjectAnimator.ofObject(endBackground, "color", |
| new ArgbEvaluator(), startColor.getColor(), endColor.getColor()); |
| if (getStartDelay() > 0) { |
| endColor.setColor(startColor.getColor()); |
| } |
| } |
| } |
| if (view instanceof TextView) { |
| TextView textView = (TextView) view; |
| int start = (Integer) startValues.values.get(PROPNAME_TEXT_COLOR); |
| int end = (Integer) endValues.values.get(PROPNAME_TEXT_COLOR); |
| if (start != end) { |
| anim = ObjectAnimator.ofObject(textView, "textColor", |
| new ArgbEvaluator(), start, end); |
| if (getStartDelay() > 0) { |
| textView.setTextColor(end); |
| } |
| } |
| } |
| return anim; |
| } |
| } |