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 | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 17 | package android.view.transition; |
| 18 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
Chet Haase | c81a849 | 2013-07-12 12:54:38 -0700 | [diff] [blame] | 21 | import android.animation.AnimatorSet; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 22 | import android.animation.ValueAnimator; |
Chet Haase | c81a849 | 2013-07-12 12:54:38 -0700 | [diff] [blame] | 23 | import android.graphics.Color; |
Chet Haase | 0873518 | 2013-06-04 10:44:40 -0700 | [diff] [blame] | 24 | import android.util.ArrayMap; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 25 | import android.view.ViewGroup; |
| 26 | import android.widget.TextView; |
| 27 | |
Chet Haase | 0873518 | 2013-06-04 10:44:40 -0700 | [diff] [blame] | 28 | import java.util.Map; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * This transition tracks changes to the text in TextView targets. If the text |
| 32 | * changes between the start and end scenes, the transition ensures that the |
| 33 | * starting text stays until the transition ends, at which point it changes |
| 34 | * to the end text. This is useful in situations where you want to resize a |
| 35 | * text view to its new size before displaying the text that goes there. |
| 36 | */ |
| 37 | public class TextChange extends Transition { |
| 38 | private static final String PROPNAME_TEXT = "android:textchange:text"; |
Chet Haase | c81a849 | 2013-07-12 12:54:38 -0700 | [diff] [blame] | 39 | private static final String PROPNAME_TEXT_COLOR = "android:textchange:textColor"; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 40 | |
Chet Haase | c81a849 | 2013-07-12 12:54:38 -0700 | [diff] [blame] | 41 | private int mChangeBehavior = CHANGE_BEHAVIOR_KEEP; |
| 42 | |
| 43 | /** |
| 44 | * Flag specifying that the text in affected/changing TextView targets will keep |
| 45 | * their original text during the transition, setting it to the final text when |
| 46 | * the transition ends. This is the default behavior. |
| 47 | * |
| 48 | * @see #setChangeBehavior(int) |
| 49 | */ |
| 50 | public static final int CHANGE_BEHAVIOR_KEEP = 0; |
| 51 | /** |
| 52 | * Flag specifying that the text changing animation should first fade |
| 53 | * out the original text completely. The new text is set on the target |
| 54 | * view at the end of the fade-out animation. This transition is typically |
| 55 | * used with a later {@link #CHANGE_BEHAVIOR_IN} transition, allowing more |
| 56 | * flexibility than the {@link #CHANGE_BEHAVIOR_OUT_IN} by allowing other |
| 57 | * transitions to be run sequentially or in parallel with these fades. |
| 58 | * |
| 59 | * @see #setChangeBehavior(int) |
| 60 | */ |
| 61 | public static final int CHANGE_BEHAVIOR_OUT = 1; |
| 62 | /** |
| 63 | * Flag specifying that the text changing animation should fade in the |
| 64 | * end text into the affected target view(s). This transition is typically |
| 65 | * used in conjunction with an earlier {@link #CHANGE_BEHAVIOR_OUT} |
| 66 | * transition, possibly with other transitions running as well, such as |
| 67 | * a sequence to fade out, then resize the view, then fade in. |
| 68 | * |
| 69 | * @see #setChangeBehavior(int) |
| 70 | */ |
| 71 | public static final int CHANGE_BEHAVIOR_IN = 2; |
| 72 | /** |
| 73 | * Flag specifying that the text changing animation should first fade |
| 74 | * out the original text completely and then fade in the |
| 75 | * new text. |
| 76 | * |
| 77 | * @see #setChangeBehavior(int) |
| 78 | */ |
| 79 | public static final int CHANGE_BEHAVIOR_OUT_IN = 3; |
| 80 | |
| 81 | /** |
| 82 | * Sets the type of changing animation that will be run, one of |
| 83 | * {@link #CHANGE_BEHAVIOR_KEEP} and {@link #CHANGE_BEHAVIOR_OUT_IN}. |
| 84 | * |
| 85 | * @param changeBehavior The type of fading animation to use when this |
| 86 | * transition is run. |
| 87 | */ |
| 88 | public void setChangeBehavior(int changeBehavior) { |
| 89 | if (changeBehavior >= CHANGE_BEHAVIOR_KEEP && changeBehavior <= CHANGE_BEHAVIOR_OUT_IN) { |
| 90 | mChangeBehavior = changeBehavior; |
| 91 | } |
| 92 | } |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 93 | |
| 94 | @Override |
| 95 | protected void captureValues(TransitionValues values, boolean start) { |
| 96 | if (values.view instanceof TextView) { |
| 97 | TextView textview = (TextView) values.view; |
| 98 | values.values.put(PROPNAME_TEXT, textview.getText()); |
Chet Haase | c81a849 | 2013-07-12 12:54:38 -0700 | [diff] [blame] | 99 | if (mChangeBehavior > CHANGE_BEHAVIOR_KEEP) { |
| 100 | values.values.put(PROPNAME_TEXT_COLOR, textview.getCurrentTextColor()); |
| 101 | } |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | @Override |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 106 | protected Animator play(ViewGroup sceneRoot, TransitionValues startValues, |
| 107 | TransitionValues endValues) { |
| 108 | if (startValues == null || endValues == null || !(endValues.view instanceof TextView)) { |
| 109 | return null; |
| 110 | } |
| 111 | final TextView view = (TextView) endValues.view; |
Chet Haase | 0873518 | 2013-06-04 10:44:40 -0700 | [diff] [blame] | 112 | Map<String, Object> startVals = startValues.values; |
| 113 | Map<String, Object> endVals = endValues.values; |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 114 | String startText = (String) startVals.get(PROPNAME_TEXT); |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 115 | final String endText = (String) endVals.get(PROPNAME_TEXT); |
| 116 | if (!startText.equals(endText)) { |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 117 | view.setText(startText); |
Chet Haase | c81a849 | 2013-07-12 12:54:38 -0700 | [diff] [blame] | 118 | Animator anim; |
| 119 | if (mChangeBehavior == CHANGE_BEHAVIOR_KEEP) { |
| 120 | anim = ValueAnimator.ofFloat(0, 1); |
| 121 | anim.addListener(new AnimatorListenerAdapter() { |
| 122 | @Override |
| 123 | public void onAnimationEnd(Animator animation) { |
| 124 | view.setText(endText); |
| 125 | } |
| 126 | }); |
| 127 | } else { |
| 128 | // Fade out start text |
| 129 | final int startColor = (Integer) startVals.get(PROPNAME_TEXT_COLOR); |
| 130 | final int endColor = (Integer) endVals.get(PROPNAME_TEXT_COLOR); |
| 131 | ValueAnimator outAnim = null, inAnim = null; |
| 132 | if (mChangeBehavior == CHANGE_BEHAVIOR_OUT_IN || |
| 133 | mChangeBehavior == CHANGE_BEHAVIOR_OUT) { |
| 134 | outAnim = ValueAnimator.ofInt(255, 0); |
| 135 | outAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| 136 | @Override |
| 137 | public void onAnimationUpdate(ValueAnimator animation) { |
| 138 | int currAlpha = (Integer) animation.getAnimatedValue(); |
| 139 | view.setTextColor(currAlpha << 24 | Color.red(startColor) << 16 | |
| 140 | Color.green(startColor) << 8 | Color.red(startColor)); |
| 141 | } |
| 142 | }); |
| 143 | outAnim.addListener(new AnimatorListenerAdapter() { |
| 144 | @Override |
| 145 | public void onAnimationEnd(Animator animation) { |
| 146 | view.setText(endText); |
| 147 | } |
| 148 | }); |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 149 | } |
Chet Haase | c81a849 | 2013-07-12 12:54:38 -0700 | [diff] [blame] | 150 | if (mChangeBehavior == CHANGE_BEHAVIOR_OUT_IN || |
| 151 | mChangeBehavior == CHANGE_BEHAVIOR_IN) { |
| 152 | inAnim = ValueAnimator.ofInt(0, 255); |
| 153 | inAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| 154 | @Override |
| 155 | public void onAnimationUpdate(ValueAnimator animation) { |
| 156 | int currAlpha = (Integer) animation.getAnimatedValue(); |
| 157 | view.setTextColor(currAlpha << 24 | Color.red(endColor) << 16 | |
| 158 | Color.green(endColor) << 8 | Color.red(endColor)); |
| 159 | } |
| 160 | }); |
| 161 | } |
| 162 | if (outAnim != null && inAnim != null) { |
| 163 | anim = new AnimatorSet(); |
| 164 | ((AnimatorSet) anim).playSequentially(outAnim, inAnim); |
| 165 | } else if (outAnim != null) { |
| 166 | anim = outAnim; |
| 167 | } else { |
| 168 | // Must be an in-only animation |
| 169 | anim = inAnim; |
| 170 | } |
| 171 | } |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 172 | return anim; |
| 173 | } |
| 174 | return null; |
| 175 | } |
| 176 | } |