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