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 */ 16 17 package android.transition; 18 19 import android.animation.Animator; 20 import android.animation.ObjectAnimator; 21 import android.content.Context; 22 import android.graphics.drawable.ColorDrawable; 23 import android.graphics.drawable.Drawable; 24 import android.util.AttributeSet; 25 import android.view.View; 26 import android.view.ViewGroup; 27 import android.widget.TextView; 28 29 /** 30 * This transition tracks changes during scene changes to the 31 * {@link View#setBackground(android.graphics.drawable.Drawable) background} 32 * property of its target views (when the background is a 33 * {@link ColorDrawable}, as well as the 34 * {@link TextView#setTextColor(android.content.res.ColorStateList) 35 * color} of the text for target TextViews. If the color changes between 36 * scenes, the color change is animated. 37 * 38 * @hide 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 public Recolor() {} 46 47 public Recolor(Context context, AttributeSet attrs) { 48 super(context, attrs); 49 } 50 51 private void captureValues(TransitionValues transitionValues) { 52 transitionValues.values.put(PROPNAME_BACKGROUND, transitionValues.view.getBackground()); 53 if (transitionValues.view instanceof TextView) { 54 transitionValues.values.put(PROPNAME_TEXT_COLOR, 55 ((TextView)transitionValues.view).getCurrentTextColor()); 56 } 57 } 58 59 @Override 60 public void captureStartValues(TransitionValues transitionValues) { 61 captureValues(transitionValues); 62 } 63 64 @Override 65 public void captureEndValues(TransitionValues transitionValues) { 66 captureValues(transitionValues); 67 } 68 69 @Override 70 public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues, 71 TransitionValues endValues) { 72 if (startValues == null || endValues == null) { 73 return null; 74 } 75 final View view = endValues.view; 76 Drawable startBackground = (Drawable) startValues.values.get(PROPNAME_BACKGROUND); 77 Drawable endBackground = (Drawable) endValues.values.get(PROPNAME_BACKGROUND); 78 boolean changed = false; 79 if (startBackground instanceof ColorDrawable && endBackground instanceof ColorDrawable) { 80 ColorDrawable startColor = (ColorDrawable) startBackground; 81 ColorDrawable endColor = (ColorDrawable) endBackground; 82 if (startColor.getColor() != endColor.getColor()) { 83 endColor.setColor(startColor.getColor()); 84 changed = true; 85 return ObjectAnimator.ofArgb(endBackground, "color", startColor.getColor(), 86 endColor.getColor()); 87 } 88 } 89 if (view instanceof TextView) { 90 TextView textView = (TextView) view; 91 int start = (Integer) startValues.values.get(PROPNAME_TEXT_COLOR); 92 int end = (Integer) endValues.values.get(PROPNAME_TEXT_COLOR); 93 if (start != end) { 94 textView.setTextColor(end); 95 changed = true; 96 return ObjectAnimator.ofArgb(textView, "textColor", start, end); 97 } 98 } 99 return null; 100 } 101 } 102