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 | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 17 | package android.transition; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 18 | |
George Mount | ecd857b | 2014-06-19 07:51:08 -0700 | [diff] [blame] | 19 | import com.android.internal.R; |
| 20 | |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 21 | import android.animation.Animator; |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 22 | import android.animation.AnimatorListenerAdapter; |
George Mount | ecd857b | 2014-06-19 07:51:08 -0700 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.res.TypedArray; |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 25 | import android.graphics.Bitmap; |
| 26 | import android.graphics.Canvas; |
| 27 | import android.graphics.drawable.BitmapDrawable; |
George Mount | ecd857b | 2014-06-19 07:51:08 -0700 | [diff] [blame] | 28 | import android.util.AttributeSet; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 29 | import android.view.View; |
| 30 | import android.view.ViewGroup; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 31 | /** |
| 32 | * This transition tracks changes to the visibility of target views in the |
| 33 | * start and end scenes. Visibility is determined not just by the |
| 34 | * {@link View#setVisibility(int)} state of views, but also whether |
| 35 | * views exist in the current view hierarchy. The class is intended to be a |
| 36 | * utility for subclasses such as {@link Fade}, which use this visibility |
| 37 | * information to determine the specific animations to run when visibility |
Chet Haase | 35a457a | 2013-08-26 07:34:12 -0700 | [diff] [blame] | 38 | * changes occur. Subclasses should implement one or both of the methods |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 39 | * {@link #onAppear(ViewGroup, TransitionValues, int, TransitionValues, int)}, |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 40 | * {@link #onDisappear(ViewGroup, TransitionValues, int, TransitionValues, int)} or |
| 41 | * {@link #onAppear(ViewGroup, View, TransitionValues, TransitionValues)}, |
| 42 | * {@link #onDisappear(ViewGroup, View, TransitionValues, TransitionValues)}. |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 43 | */ |
| 44 | public abstract class Visibility extends Transition { |
| 45 | |
George Mount | 6ceac2e | 2014-07-31 09:58:16 -0700 | [diff] [blame] | 46 | static final String PROPNAME_VISIBILITY = "android:visibility:visibility"; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 47 | private static final String PROPNAME_PARENT = "android:visibility:parent"; |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 48 | private static final String PROPNAME_SCREEN_LOCATION = "android:visibility:screenLocation"; |
| 49 | |
George Mount | 18ab799 | 2014-06-25 17:04:51 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Mode used in {@link #setMode(int)} to make the transition |
| 52 | * operate on targets that are appearing. Maybe be combined with |
George Mount | ad88e1b | 2014-07-18 15:41:13 -0700 | [diff] [blame] | 53 | * {@link #MODE_OUT} to target Visibility changes both in and out. |
George Mount | 18ab799 | 2014-06-25 17:04:51 -0700 | [diff] [blame] | 54 | */ |
George Mount | ad88e1b | 2014-07-18 15:41:13 -0700 | [diff] [blame] | 55 | public static final int MODE_IN = 0x1; |
George Mount | 18ab799 | 2014-06-25 17:04:51 -0700 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * Mode used in {@link #setMode(int)} to make the transition |
| 59 | * operate on targets that are disappearing. Maybe be combined with |
George Mount | ad88e1b | 2014-07-18 15:41:13 -0700 | [diff] [blame] | 60 | * {@link #MODE_IN} to target Visibility changes both in and out. |
George Mount | 18ab799 | 2014-06-25 17:04:51 -0700 | [diff] [blame] | 61 | */ |
George Mount | ad88e1b | 2014-07-18 15:41:13 -0700 | [diff] [blame] | 62 | public static final int MODE_OUT = 0x2; |
George Mount | 18ab799 | 2014-06-25 17:04:51 -0700 | [diff] [blame] | 63 | |
Chet Haase | af78bdd | 2013-08-27 16:06:26 -0700 | [diff] [blame] | 64 | private static final String[] sTransitionProperties = { |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 65 | PROPNAME_VISIBILITY, |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 66 | }; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 67 | |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 68 | private static class VisibilityInfo { |
| 69 | boolean visibilityChange; |
| 70 | boolean fadeIn; |
| 71 | int startVisibility; |
| 72 | int endVisibility; |
Chet Haase | 35a457a | 2013-08-26 07:34:12 -0700 | [diff] [blame] | 73 | ViewGroup startParent; |
| 74 | ViewGroup endParent; |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 75 | } |
| 76 | |
George Mount | ad88e1b | 2014-07-18 15:41:13 -0700 | [diff] [blame] | 77 | private int mMode = MODE_IN | MODE_OUT; |
George Mount | 18ab799 | 2014-06-25 17:04:51 -0700 | [diff] [blame] | 78 | |
George Mount | b5ef7f8 | 2014-07-09 14:55:03 -0700 | [diff] [blame] | 79 | private int mForcedStartVisibility = -1; |
| 80 | private int mForcedEndVisibility = -1; |
| 81 | |
George Mount | ecd857b | 2014-06-19 07:51:08 -0700 | [diff] [blame] | 82 | public Visibility() {} |
| 83 | |
| 84 | public Visibility(Context context, AttributeSet attrs) { |
| 85 | super(context, attrs); |
| 86 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.VisibilityTransition); |
Craig Mautner | 3b2cd1d | 2014-08-25 14:25:54 -0700 | [diff] [blame] | 87 | int mode = a.getInt(R.styleable.VisibilityTransition_transitionVisibilityMode, 0); |
George Mount | ecd857b | 2014-06-19 07:51:08 -0700 | [diff] [blame] | 88 | a.recycle(); |
| 89 | if (mode != 0) { |
| 90 | setMode(mode); |
| 91 | } |
| 92 | } |
| 93 | |
George Mount | 18ab799 | 2014-06-25 17:04:51 -0700 | [diff] [blame] | 94 | /** |
| 95 | * Changes the transition to support appearing and/or disappearing Views, depending |
| 96 | * on <code>mode</code>. |
| 97 | * |
| 98 | * @param mode The behavior supported by this transition, a combination of |
George Mount | ad88e1b | 2014-07-18 15:41:13 -0700 | [diff] [blame] | 99 | * {@link #MODE_IN} and {@link #MODE_OUT}. |
Craig Mautner | 3b2cd1d | 2014-08-25 14:25:54 -0700 | [diff] [blame] | 100 | * @attr ref android.R.styleable#VisibilityTransition_transitionVisibilityMode |
George Mount | 18ab799 | 2014-06-25 17:04:51 -0700 | [diff] [blame] | 101 | */ |
| 102 | public void setMode(int mode) { |
George Mount | ad88e1b | 2014-07-18 15:41:13 -0700 | [diff] [blame] | 103 | if ((mode & ~(MODE_IN | MODE_OUT)) != 0) { |
| 104 | throw new IllegalArgumentException("Only MODE_IN and MODE_OUT flags are allowed"); |
George Mount | 18ab799 | 2014-06-25 17:04:51 -0700 | [diff] [blame] | 105 | } |
| 106 | mMode = mode; |
| 107 | } |
| 108 | |
George Mount | ecd857b | 2014-06-19 07:51:08 -0700 | [diff] [blame] | 109 | /** |
| 110 | * Returns whether appearing and/or disappearing Views are supported. |
| 111 | * |
| 112 | * Returns whether appearing and/or disappearing Views are supported. A combination of |
George Mount | ad88e1b | 2014-07-18 15:41:13 -0700 | [diff] [blame] | 113 | * {@link #MODE_IN} and {@link #MODE_OUT}. |
Craig Mautner | 3b2cd1d | 2014-08-25 14:25:54 -0700 | [diff] [blame] | 114 | * @attr ref android.R.styleable#VisibilityTransition_transitionVisibilityMode |
George Mount | ecd857b | 2014-06-19 07:51:08 -0700 | [diff] [blame] | 115 | */ |
| 116 | public int getMode() { |
| 117 | return mMode; |
| 118 | } |
| 119 | |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 120 | @Override |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 121 | public String[] getTransitionProperties() { |
| 122 | return sTransitionProperties; |
| 123 | } |
| 124 | |
George Mount | b5ef7f8 | 2014-07-09 14:55:03 -0700 | [diff] [blame] | 125 | private void captureValues(TransitionValues transitionValues, int forcedVisibility) { |
| 126 | int visibility; |
| 127 | if (forcedVisibility != -1) { |
| 128 | visibility = forcedVisibility; |
| 129 | } else { |
| 130 | visibility = transitionValues.view.getVisibility(); |
| 131 | } |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 132 | transitionValues.values.put(PROPNAME_VISIBILITY, visibility); |
| 133 | transitionValues.values.put(PROPNAME_PARENT, transitionValues.view.getParent()); |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 134 | int[] loc = new int[2]; |
| 135 | transitionValues.view.getLocationOnScreen(loc); |
| 136 | transitionValues.values.put(PROPNAME_SCREEN_LOCATION, loc); |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 139 | @Override |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 140 | public void captureStartValues(TransitionValues transitionValues) { |
George Mount | b5ef7f8 | 2014-07-09 14:55:03 -0700 | [diff] [blame] | 141 | captureValues(transitionValues, mForcedStartVisibility); |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void captureEndValues(TransitionValues transitionValues) { |
George Mount | b5ef7f8 | 2014-07-09 14:55:03 -0700 | [diff] [blame] | 146 | captureValues(transitionValues, mForcedEndVisibility); |
| 147 | } |
| 148 | |
| 149 | /** @hide */ |
| 150 | @Override |
| 151 | public void forceVisibility(int visibility, boolean isStartValue) { |
| 152 | if (isStartValue) { |
| 153 | mForcedStartVisibility = visibility; |
| 154 | } else { |
| 155 | mForcedEndVisibility = visibility; |
| 156 | } |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 159 | /** |
| 160 | * Returns whether the view is 'visible' according to the given values |
| 161 | * object. This is determined by testing the same properties in the values |
| 162 | * object that are used to determine whether the object is appearing or |
| 163 | * disappearing in the {@link |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 164 | * Transition#createAnimator(ViewGroup, TransitionValues, TransitionValues)} |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 165 | * method. This method can be called by, for example, subclasses that want |
| 166 | * to know whether the object is visible in the same way that Visibility |
| 167 | * determines it for the actual animation. |
| 168 | * |
| 169 | * @param values The TransitionValues object that holds the information by |
| 170 | * which visibility is determined. |
| 171 | * @return True if the view reference by <code>values</code> is visible, |
| 172 | * false otherwise. |
| 173 | */ |
| 174 | public boolean isVisible(TransitionValues values) { |
| 175 | if (values == null) { |
| 176 | return false; |
| 177 | } |
| 178 | int visibility = (Integer) values.values.get(PROPNAME_VISIBILITY); |
| 179 | View parent = (View) values.values.get(PROPNAME_PARENT); |
| 180 | |
| 181 | return visibility == View.VISIBLE && parent != null; |
| 182 | } |
| 183 | |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 184 | private VisibilityInfo getVisibilityChangeInfo(TransitionValues startValues, |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 185 | TransitionValues endValues) { |
Chet Haase | 7660d12 | 2013-09-13 13:29:31 -0700 | [diff] [blame] | 186 | final VisibilityInfo visInfo = new VisibilityInfo(); |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 187 | visInfo.visibilityChange = false; |
| 188 | visInfo.fadeIn = false; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 189 | if (startValues != null && startValues.values.containsKey(PROPNAME_VISIBILITY)) { |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 190 | visInfo.startVisibility = (Integer) startValues.values.get(PROPNAME_VISIBILITY); |
Chet Haase | 35a457a | 2013-08-26 07:34:12 -0700 | [diff] [blame] | 191 | visInfo.startParent = (ViewGroup) startValues.values.get(PROPNAME_PARENT); |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 192 | } else { |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 193 | visInfo.startVisibility = -1; |
| 194 | visInfo.startParent = null; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 195 | } |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 196 | if (endValues != null && endValues.values.containsKey(PROPNAME_VISIBILITY)) { |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 197 | visInfo.endVisibility = (Integer) endValues.values.get(PROPNAME_VISIBILITY); |
Chet Haase | 35a457a | 2013-08-26 07:34:12 -0700 | [diff] [blame] | 198 | visInfo.endParent = (ViewGroup) endValues.values.get(PROPNAME_PARENT); |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 199 | } else { |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 200 | visInfo.endVisibility = -1; |
| 201 | visInfo.endParent = null; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 202 | } |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 203 | if (startValues != null && endValues != null) { |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 204 | if (visInfo.startVisibility == visInfo.endVisibility && |
| 205 | visInfo.startParent == visInfo.endParent) { |
| 206 | return visInfo; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 207 | } else { |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 208 | if (visInfo.startVisibility != visInfo.endVisibility) { |
| 209 | if (visInfo.startVisibility == View.VISIBLE) { |
| 210 | visInfo.fadeIn = false; |
| 211 | visInfo.visibilityChange = true; |
| 212 | } else if (visInfo.endVisibility == View.VISIBLE) { |
| 213 | visInfo.fadeIn = true; |
| 214 | visInfo.visibilityChange = true; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 215 | } |
| 216 | // no visibilityChange if going between INVISIBLE and GONE |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 217 | } else if (visInfo.startParent != visInfo.endParent) { |
| 218 | if (visInfo.endParent == null) { |
| 219 | visInfo.fadeIn = false; |
| 220 | visInfo.visibilityChange = true; |
| 221 | } else if (visInfo.startParent == null) { |
| 222 | visInfo.fadeIn = true; |
| 223 | visInfo.visibilityChange = true; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | } |
George Mount | 79b2781 | 2014-09-12 16:39:04 -0700 | [diff] [blame] | 227 | } else if (startValues == null && visInfo.endVisibility == View.VISIBLE) { |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 228 | visInfo.fadeIn = true; |
| 229 | visInfo.visibilityChange = true; |
George Mount | 79b2781 | 2014-09-12 16:39:04 -0700 | [diff] [blame] | 230 | } else if (endValues == null && visInfo.startVisibility == View.VISIBLE) { |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 231 | visInfo.fadeIn = false; |
| 232 | visInfo.visibilityChange = true; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 233 | } |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 234 | return visInfo; |
| 235 | } |
| 236 | |
| 237 | @Override |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 238 | public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues, |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 239 | TransitionValues endValues) { |
| 240 | VisibilityInfo visInfo = getVisibilityChangeInfo(startValues, endValues); |
George Mount | 30da61d | 2014-05-09 13:17:52 -0700 | [diff] [blame] | 241 | if (visInfo.visibilityChange |
| 242 | && (visInfo.startParent != null || visInfo.endParent != null)) { |
| 243 | if (visInfo.fadeIn) { |
| 244 | return onAppear(sceneRoot, startValues, visInfo.startVisibility, |
| 245 | endValues, visInfo.endVisibility); |
| 246 | } else { |
| 247 | return onDisappear(sceneRoot, startValues, visInfo.startVisibility, |
| 248 | endValues, visInfo.endVisibility |
| 249 | ); |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 250 | } |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 251 | } |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 252 | return null; |
| 253 | } |
| 254 | |
| 255 | /** |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 256 | * The default implementation of this method calls |
| 257 | * {@link #onAppear(ViewGroup, View, TransitionValues, TransitionValues)}. |
| 258 | * Subclasses should override this method or |
| 259 | * {@link #onAppear(ViewGroup, View, TransitionValues, TransitionValues)}. |
| 260 | * if they need to create an Animator when targets appear. |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 261 | * The method should only be called by the Visibility class; it is |
| 262 | * not intended to be called from external classes. |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 263 | * |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 264 | * @param sceneRoot The root of the transition hierarchy |
| 265 | * @param startValues The target values in the start scene |
| 266 | * @param startVisibility The target visibility in the start scene |
| 267 | * @param endValues The target values in the end scene |
| 268 | * @param endVisibility The target visibility in the end scene |
| 269 | * @return An Animator to be started at the appropriate time in the |
| 270 | * overall transition for this scene change. A null value means no animation |
| 271 | * should be run. |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 272 | */ |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 273 | public Animator onAppear(ViewGroup sceneRoot, |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 274 | TransitionValues startValues, int startVisibility, |
| 275 | TransitionValues endValues, int endVisibility) { |
George Mount | ad88e1b | 2014-07-18 15:41:13 -0700 | [diff] [blame] | 276 | if ((mMode & MODE_IN) != MODE_IN || endValues == null) { |
George Mount | 18ab799 | 2014-06-25 17:04:51 -0700 | [diff] [blame] | 277 | return null; |
| 278 | } |
Craig Stout | e988ee1 | 2014-09-10 15:53:29 -0700 | [diff] [blame] | 279 | if (startValues == null) { |
| 280 | VisibilityInfo parentVisibilityInfo = null; |
| 281 | View endParent = (View) endValues.view.getParent(); |
| 282 | TransitionValues startParentValues = getMatchedTransitionValues(endParent, |
| 283 | false); |
| 284 | TransitionValues endParentValues = getTransitionValues(endParent, false); |
| 285 | parentVisibilityInfo = |
| 286 | getVisibilityChangeInfo(startParentValues, endParentValues); |
| 287 | if (parentVisibilityInfo.visibilityChange) { |
| 288 | return null; |
| 289 | } |
| 290 | } |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 291 | return onAppear(sceneRoot, endValues.view, startValues, endValues); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * The default implementation of this method returns a null Animator. Subclasses should |
| 296 | * override this method to make targets appear with the desired transition. The |
| 297 | * method should only be called from |
| 298 | * {@link #onAppear(ViewGroup, TransitionValues, int, TransitionValues, int)}. |
| 299 | * |
| 300 | * @param sceneRoot The root of the transition hierarchy |
| 301 | * @param view The View to make appear. This will be in the target scene's View hierarchy and |
| 302 | * will be VISIBLE. |
| 303 | * @param startValues The target values in the start scene |
| 304 | * @param endValues The target values in the end scene |
| 305 | * @return An Animator to be started at the appropriate time in the |
| 306 | * overall transition for this scene change. A null value means no animation |
| 307 | * should be run. |
| 308 | */ |
| 309 | public Animator onAppear(ViewGroup sceneRoot, View view, TransitionValues startValues, |
| 310 | TransitionValues endValues) { |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 311 | return null; |
| 312 | } |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 313 | |
| 314 | /** |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 315 | * Subclasses should override this method or |
| 316 | * {@link #onDisappear(ViewGroup, View, TransitionValues, TransitionValues)} |
| 317 | * if they need to create an Animator when targets disappear. |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 318 | * The method should only be called by the Visibility class; it is |
| 319 | * not intended to be called from external classes. |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 320 | * <p> |
| 321 | * The default implementation of this method attempts to find a View to use to call |
| 322 | * {@link #onDisappear(ViewGroup, View, TransitionValues, TransitionValues)}, |
| 323 | * based on the situation of the View in the View hierarchy. For example, |
| 324 | * if a View was simply removed from its parent, then the View will be added |
| 325 | * into a {@link android.view.ViewGroupOverlay} and passed as the <code>view</code> |
| 326 | * parameter in {@link #onDisappear(ViewGroup, View, TransitionValues, TransitionValues)}. |
| 327 | * If a visible View is changed to be {@link View#GONE} or {@link View#INVISIBLE}, |
| 328 | * then it can be used as the <code>view</code> and the visibility will be changed |
| 329 | * to {@link View#VISIBLE} for the duration of the animation. However, if a View |
| 330 | * is in a hierarchy which is also altering its visibility, the situation can be |
| 331 | * more complicated. In general, if a view that is no longer in the hierarchy in |
| 332 | * the end scene still has a parent (so its parent hierarchy was removed, but it |
| 333 | * was not removed from its parent), then it will be left alone to avoid side-effects from |
| 334 | * improperly removing it from its parent. The only exception to this is if |
| 335 | * the previous {@link Scene} was {@link Scene#getSceneForLayout(ViewGroup, int, |
| 336 | * android.content.Context) created from a layout resource file}, then it is considered |
| 337 | * safe to un-parent the starting scene view in order to make it disappear.</p> |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 338 | * |
| 339 | * @param sceneRoot The root of the transition hierarchy |
| 340 | * @param startValues The target values in the start scene |
| 341 | * @param startVisibility The target visibility in the start scene |
| 342 | * @param endValues The target values in the end scene |
| 343 | * @param endVisibility The target visibility in the end scene |
| 344 | * @return An Animator to be started at the appropriate time in the |
| 345 | * overall transition for this scene change. A null value means no animation |
| 346 | * should be run. |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 347 | */ |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 348 | public Animator onDisappear(ViewGroup sceneRoot, |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 349 | TransitionValues startValues, int startVisibility, |
| 350 | TransitionValues endValues, int endVisibility) { |
George Mount | ad88e1b | 2014-07-18 15:41:13 -0700 | [diff] [blame] | 351 | if ((mMode & MODE_OUT) != MODE_OUT) { |
George Mount | 18ab799 | 2014-06-25 17:04:51 -0700 | [diff] [blame] | 352 | return null; |
| 353 | } |
| 354 | |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 355 | View startView = (startValues != null) ? startValues.view : null; |
| 356 | View endView = (endValues != null) ? endValues.view : null; |
| 357 | View overlayView = null; |
| 358 | View viewToKeep = null; |
| 359 | if (endView == null || endView.getParent() == null) { |
| 360 | if (endView != null) { |
| 361 | // endView was removed from its parent - add it to the overlay |
| 362 | overlayView = endView; |
| 363 | } else if (startView != null) { |
| 364 | // endView does not exist. Use startView only under certain |
| 365 | // conditions, because placing a view in an overlay necessitates |
| 366 | // it being removed from its current parent |
| 367 | if (startView.getParent() == null) { |
| 368 | // no parent - safe to use |
| 369 | overlayView = startView; |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 370 | } else if (startView.getParent() instanceof View) { |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 371 | View startParent = (View) startView.getParent(); |
George Mount | 79b2781 | 2014-09-12 16:39:04 -0700 | [diff] [blame] | 372 | TransitionValues startParentValues = getTransitionValues(startParent, true); |
George Mount | 9f1ac39 | 2014-09-07 15:04:03 -0700 | [diff] [blame] | 373 | TransitionValues endParentValues = getMatchedTransitionValues(startParent, |
| 374 | true); |
George Mount | 79b2781 | 2014-09-12 16:39:04 -0700 | [diff] [blame] | 375 | VisibilityInfo parentVisibilityInfo = |
| 376 | getVisibilityChangeInfo(startParentValues, endParentValues); |
| 377 | if (!parentVisibilityInfo.visibilityChange) { |
| 378 | overlayView = TransitionUtils.copyViewImage(sceneRoot, startView, |
| 379 | startParent); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 380 | } else if (startParent.getParent() == null) { |
| 381 | int id = startParent.getId(); |
| 382 | if (id != View.NO_ID && sceneRoot.findViewById(id) != null |
| 383 | && mCanRemoveViews) { |
| 384 | // no parent, but its parent is unparented but the parent |
| 385 | // hierarchy has been replaced by a new hierarchy with the same id |
| 386 | // and it is safe to un-parent startView |
| 387 | overlayView = startView; |
| 388 | } |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | } |
| 392 | } else { |
| 393 | // visibility change |
| 394 | if (endVisibility == View.INVISIBLE) { |
| 395 | viewToKeep = endView; |
| 396 | } else { |
| 397 | // Becoming GONE |
| 398 | if (startView == endView) { |
| 399 | viewToKeep = endView; |
| 400 | } else { |
| 401 | overlayView = startView; |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | final int finalVisibility = endVisibility; |
| 406 | final ViewGroup finalSceneRoot = sceneRoot; |
| 407 | |
| 408 | if (overlayView != null) { |
| 409 | // TODO: Need to do this for general case of adding to overlay |
| 410 | int[] screenLoc = (int[]) startValues.values.get(PROPNAME_SCREEN_LOCATION); |
| 411 | int screenX = screenLoc[0]; |
| 412 | int screenY = screenLoc[1]; |
| 413 | int[] loc = new int[2]; |
| 414 | sceneRoot.getLocationOnScreen(loc); |
| 415 | overlayView.offsetLeftAndRight((screenX - loc[0]) - overlayView.getLeft()); |
| 416 | overlayView.offsetTopAndBottom((screenY - loc[1]) - overlayView.getTop()); |
| 417 | sceneRoot.getOverlay().add(overlayView); |
| 418 | Animator animator = onDisappear(sceneRoot, overlayView, startValues, endValues); |
| 419 | if (animator == null) { |
| 420 | sceneRoot.getOverlay().remove(overlayView); |
| 421 | } else { |
| 422 | final View finalOverlayView = overlayView; |
| 423 | animator.addListener(new AnimatorListenerAdapter() { |
| 424 | @Override |
| 425 | public void onAnimationEnd(Animator animation) { |
| 426 | finalSceneRoot.getOverlay().remove(finalOverlayView); |
| 427 | } |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 428 | }); |
| 429 | } |
| 430 | return animator; |
| 431 | } |
| 432 | |
| 433 | if (viewToKeep != null) { |
George Mount | b5ef7f8 | 2014-07-09 14:55:03 -0700 | [diff] [blame] | 434 | int originalVisibility = -1; |
| 435 | final boolean isForcedVisibility = mForcedStartVisibility != -1 || |
| 436 | mForcedEndVisibility != -1; |
| 437 | if (!isForcedVisibility) { |
| 438 | originalVisibility = viewToKeep.getVisibility(); |
| 439 | viewToKeep.setVisibility(View.VISIBLE); |
| 440 | } |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 441 | Animator animator = onDisappear(sceneRoot, viewToKeep, startValues, endValues); |
George Mount | b5ef7f8 | 2014-07-09 14:55:03 -0700 | [diff] [blame] | 442 | if (animator != null) { |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 443 | final View finalViewToKeep = viewToKeep; |
| 444 | animator.addListener(new AnimatorListenerAdapter() { |
| 445 | boolean mCanceled = false; |
| 446 | |
| 447 | @Override |
| 448 | public void onAnimationPause(Animator animation) { |
George Mount | b5ef7f8 | 2014-07-09 14:55:03 -0700 | [diff] [blame] | 449 | if (!mCanceled && !isForcedVisibility) { |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 450 | finalViewToKeep.setVisibility(finalVisibility); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | @Override |
| 455 | public void onAnimationResume(Animator animation) { |
George Mount | b5ef7f8 | 2014-07-09 14:55:03 -0700 | [diff] [blame] | 456 | if (!mCanceled && !isForcedVisibility) { |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 457 | finalViewToKeep.setVisibility(View.VISIBLE); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | @Override |
| 462 | public void onAnimationCancel(Animator animation) { |
| 463 | mCanceled = true; |
| 464 | } |
| 465 | |
| 466 | @Override |
| 467 | public void onAnimationEnd(Animator animation) { |
| 468 | if (!mCanceled) { |
George Mount | b5ef7f8 | 2014-07-09 14:55:03 -0700 | [diff] [blame] | 469 | if (isForcedVisibility) { |
| 470 | finalViewToKeep.setTransitionAlpha(0); |
| 471 | } else { |
| 472 | finalViewToKeep.setVisibility(finalVisibility); |
| 473 | } |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | }); |
George Mount | b5ef7f8 | 2014-07-09 14:55:03 -0700 | [diff] [blame] | 477 | } else if (!isForcedVisibility) { |
| 478 | viewToKeep.setVisibility(originalVisibility); |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 479 | } |
| 480 | return animator; |
| 481 | } |
| 482 | return null; |
| 483 | } |
| 484 | |
George Mount | 4c20ea2 | 2014-06-17 10:14:39 -0700 | [diff] [blame] | 485 | @Override |
| 486 | boolean areValuesChanged(TransitionValues oldValues, TransitionValues newValues) { |
| 487 | VisibilityInfo changeInfo = getVisibilityChangeInfo(oldValues, newValues); |
| 488 | if (oldValues == null && newValues == null) { |
| 489 | return false; |
| 490 | } |
| 491 | return changeInfo.visibilityChange && (changeInfo.startVisibility == View.VISIBLE || |
| 492 | changeInfo.endVisibility == View.VISIBLE); |
| 493 | } |
| 494 | |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 495 | /** |
| 496 | * The default implementation of this method returns a null Animator. Subclasses should |
| 497 | * override this method to make targets disappear with the desired transition. The |
| 498 | * method should only be called from |
| 499 | * {@link #onDisappear(ViewGroup, TransitionValues, int, TransitionValues, int)}. |
| 500 | * |
| 501 | * @param sceneRoot The root of the transition hierarchy |
| 502 | * @param view The View to make disappear. This will be in the target scene's View |
| 503 | * hierarchy or in an {@link android.view.ViewGroupOverlay} and will be |
| 504 | * VISIBLE. |
| 505 | * @param startValues The target values in the start scene |
| 506 | * @param endValues The target values in the end scene |
| 507 | * @return An Animator to be started at the appropriate time in the |
| 508 | * overall transition for this scene change. A null value means no animation |
| 509 | * should be run. |
| 510 | */ |
| 511 | public Animator onDisappear(ViewGroup sceneRoot, View view, TransitionValues startValues, |
| 512 | TransitionValues endValues) { |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 513 | return null; |
| 514 | } |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 515 | } |