• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.content.Context;
24 import android.graphics.drawable.ColorDrawable;
25 import android.graphics.drawable.Drawable;
26 import android.util.AttributeSet;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.TextView;
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  * @hide
41  */
42 public class Recolor extends Transition {
43 
44     private static final String PROPNAME_BACKGROUND = "android:recolor:background";
45     private static final String PROPNAME_TEXT_COLOR = "android:recolor:textColor";
46 
Recolor()47     public Recolor() {}
48 
Recolor(Context context, AttributeSet attrs)49     public Recolor(Context context, AttributeSet attrs) {
50         super(context, attrs);
51     }
52 
captureValues(TransitionValues transitionValues)53     private void captureValues(TransitionValues transitionValues) {
54         transitionValues.values.put(PROPNAME_BACKGROUND, transitionValues.view.getBackground());
55         if (transitionValues.view instanceof TextView) {
56             transitionValues.values.put(PROPNAME_TEXT_COLOR,
57                     ((TextView)transitionValues.view).getCurrentTextColor());
58         }
59     }
60 
61     @Override
captureStartValues(TransitionValues transitionValues)62     public void captureStartValues(TransitionValues transitionValues) {
63         captureValues(transitionValues);
64     }
65 
66     @Override
captureEndValues(TransitionValues transitionValues)67     public void captureEndValues(TransitionValues transitionValues) {
68         captureValues(transitionValues);
69     }
70 
71     @Nullable
72     @Override
createAnimator(@onNull ViewGroup sceneRoot, @Nullable TransitionValues startValues, @Nullable TransitionValues endValues)73     public Animator createAnimator(@NonNull ViewGroup sceneRoot,
74             @Nullable TransitionValues startValues,
75             @Nullable TransitionValues endValues) {
76         if (startValues == null || endValues == null) {
77             return null;
78         }
79         final View view = endValues.view;
80         Drawable startBackground = (Drawable) startValues.values.get(PROPNAME_BACKGROUND);
81         Drawable endBackground = (Drawable) endValues.values.get(PROPNAME_BACKGROUND);
82         boolean changed = false;
83         if (startBackground instanceof ColorDrawable && endBackground instanceof ColorDrawable) {
84             ColorDrawable startColor = (ColorDrawable) startBackground;
85             ColorDrawable endColor = (ColorDrawable) endBackground;
86             if (startColor.getColor() != endColor.getColor()) {
87                 endColor.setColor(startColor.getColor());
88                 changed = true;
89                 return ObjectAnimator.ofArgb(endBackground, "color", startColor.getColor(),
90                         endColor.getColor());
91             }
92         }
93         if (view instanceof TextView) {
94             TextView textView = (TextView) view;
95             int start = (Integer) startValues.values.get(PROPNAME_TEXT_COLOR);
96             int end = (Integer) endValues.values.get(PROPNAME_TEXT_COLOR);
97             if (start != end) {
98                 textView.setTextColor(end);
99                 changed = true;
100                 return ObjectAnimator.ofArgb(textView, "textColor", start, end);
101             }
102         }
103         return null;
104     }
105 }
106