Home | History | Annotate | Download | only in transition
      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.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 
     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.
     36  *
     37  * @hide
     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 
     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());
     49         }
     50     }
     51 
     52     @Override
     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,
     64             TransitionValues endValues) {
     65         if (startValues == null || endValues == null) {
     66             return null;
     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;
     78                 return ObjectAnimator.ofObject(endBackground, "color",
     79                         new ArgbEvaluator(), startColor.getColor(), endColor.getColor());
     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;
     89                 return ObjectAnimator.ofObject(textView, "textColor",
     90                         new ArgbEvaluator(), start, end);
     91             }
     92         }
     93         return null;
     94     }
     95 }
     96