George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | package android.app; |
| 17 | |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 18 | import android.content.Context; |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 19 | import android.graphics.Matrix; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 20 | import android.graphics.Rect; |
Dake Gu | 7bf379c | 2014-07-15 16:29:38 -0700 | [diff] [blame] | 21 | import android.graphics.RectF; |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 22 | import android.os.Bundle; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 23 | import android.os.Handler; |
George Mount | 480ca82 | 2014-08-08 16:35:48 -0700 | [diff] [blame] | 24 | import android.os.Parcelable; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 25 | import android.os.ResultReceiver; |
| 26 | import android.transition.Transition; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 27 | import android.transition.TransitionSet; |
| 28 | import android.util.ArrayMap; |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 29 | import android.view.GhostView; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 30 | import android.view.View; |
| 31 | import android.view.ViewGroup; |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 32 | import android.view.ViewGroupOverlay; |
| 33 | import android.view.ViewParent; |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 34 | import android.view.ViewRootImpl; |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 35 | import android.view.ViewTreeObserver; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 36 | import android.view.Window; |
George Mount | caa0310 | 2014-04-15 09:01:32 -0700 | [diff] [blame] | 37 | import android.widget.ImageView; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 38 | |
| 39 | import java.util.ArrayList; |
| 40 | import java.util.Collection; |
| 41 | |
| 42 | /** |
| 43 | * Base class for ExitTransitionCoordinator and EnterTransitionCoordinator, classes |
| 44 | * that manage activity transitions and the communications coordinating them between |
| 45 | * Activities. The ExitTransitionCoordinator is created in the |
| 46 | * ActivityOptions#makeSceneTransitionAnimation. The EnterTransitionCoordinator |
| 47 | * is created by ActivityOptions#createEnterActivityTransition by Activity when the window is |
| 48 | * attached. |
| 49 | * |
| 50 | * Typical startActivity goes like this: |
| 51 | * 1) ExitTransitionCoordinator created with ActivityOptions#makeSceneTransitionAnimation |
| 52 | * 2) Activity#startActivity called and that calls startExit() through |
| 53 | * ActivityOptions#dispatchStartExit |
| 54 | * - Exit transition starts by setting transitioning Views to INVISIBLE |
| 55 | * 3) Launched Activity starts, creating an EnterTransitionCoordinator. |
| 56 | * - The Window is made translucent |
| 57 | * - The Window background alpha is set to 0 |
| 58 | * - The transitioning views are made INVISIBLE |
| 59 | * - MSG_SET_LISTENER is sent back to the ExitTransitionCoordinator. |
| 60 | * 4) The shared element transition completes. |
| 61 | * - MSG_TAKE_SHARED_ELEMENTS is sent to the EnterTransitionCoordinator |
| 62 | * 5) The MSG_TAKE_SHARED_ELEMENTS is received by the EnterTransitionCoordinator. |
| 63 | * - Shared elements are made VISIBLE |
| 64 | * - Shared elements positions and size are set to match the end state of the calling |
| 65 | * Activity. |
| 66 | * - The shared element transition is started |
| 67 | * - If the window allows overlapping transitions, the views transition is started by setting |
| 68 | * the entering Views to VISIBLE and the background alpha is animated to opaque. |
| 69 | * - MSG_HIDE_SHARED_ELEMENTS is sent to the ExitTransitionCoordinator |
| 70 | * 6) MSG_HIDE_SHARED_ELEMENTS is received by the ExitTransitionCoordinator |
| 71 | * - The shared elements are made INVISIBLE |
| 72 | * 7) The exit transition completes in the calling Activity. |
| 73 | * - MSG_EXIT_TRANSITION_COMPLETE is sent to the EnterTransitionCoordinator. |
| 74 | * 8) The MSG_EXIT_TRANSITION_COMPLETE is received by the EnterTransitionCoordinator. |
| 75 | * - If the window doesn't allow overlapping enter transitions, the enter transition is started |
| 76 | * by setting entering views to VISIBLE and the background is animated to opaque. |
| 77 | * 9) The background opacity animation completes. |
| 78 | * - The window is made opaque |
| 79 | * 10) The calling Activity gets an onStop() call |
| 80 | * - onActivityStopped() is called and all exited Views are made VISIBLE. |
| 81 | * |
Craig Mautner | 73f843d | 2014-05-19 09:42:28 -0700 | [diff] [blame] | 82 | * Typical finishAfterTransition goes like this: |
| 83 | * 1) finishAfterTransition() creates an ExitTransitionCoordinator and calls startExit() |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 84 | * - The Window start transitioning to Translucent with a new ActivityOptions. |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 85 | * - If no background exists, a black background is substituted |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 86 | * - The shared elements in the scene are matched against those shared elements |
| 87 | * that were sent by comparing the names. |
| 88 | * - The exit transition is started by setting Views to INVISIBLE. |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 89 | * 2) The ActivityOptions is received by the Activity and an EnterTransitionCoordinator is created. |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 90 | * - All transitioning views are made VISIBLE to reverse what was done when onActivityStopped() |
| 91 | * was called |
| 92 | * 3) The Window is made translucent and a callback is received |
| 93 | * - The background alpha is animated to 0 |
| 94 | * 4) The background alpha animation completes |
| 95 | * 5) The shared element transition completes |
| 96 | * - After both 4 & 5 complete, MSG_TAKE_SHARED_ELEMENTS is sent to the |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 97 | * EnterTransitionCoordinator |
| 98 | * 6) MSG_TAKE_SHARED_ELEMENTS is received by EnterTransitionCoordinator |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 99 | * - Shared elements are made VISIBLE |
| 100 | * - Shared elements positions and size are set to match the end state of the calling |
| 101 | * Activity. |
| 102 | * - The shared element transition is started |
| 103 | * - If the window allows overlapping transitions, the views transition is started by setting |
| 104 | * the entering Views to VISIBLE. |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 105 | * - MSG_HIDE_SHARED_ELEMENTS is sent to the ExitTransitionCoordinator |
| 106 | * 7) MSG_HIDE_SHARED_ELEMENTS is received by the ExitTransitionCoordinator |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 107 | * - The shared elements are made INVISIBLE |
| 108 | * 8) The exit transition completes in the finishing Activity. |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 109 | * - MSG_EXIT_TRANSITION_COMPLETE is sent to the EnterTransitionCoordinator. |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 110 | * - finish() is called on the exiting Activity |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 111 | * 9) The MSG_EXIT_TRANSITION_COMPLETE is received by the EnterTransitionCoordinator. |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 112 | * - If the window doesn't allow overlapping enter transitions, the enter transition is started |
| 113 | * by setting entering views to VISIBLE. |
| 114 | */ |
| 115 | abstract class ActivityTransitionCoordinator extends ResultReceiver { |
| 116 | private static final String TAG = "ActivityTransitionCoordinator"; |
| 117 | |
| 118 | /** |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 119 | * For Activity transitions, the called Activity's listener to receive calls |
| 120 | * when transitions complete. |
| 121 | */ |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 122 | static final String KEY_REMOTE_RECEIVER = "android:remoteReceiver"; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 123 | |
Dake Gu | 7bf379c | 2014-07-15 16:29:38 -0700 | [diff] [blame] | 124 | protected static final String KEY_SCREEN_LEFT = "shared_element:screenLeft"; |
| 125 | protected static final String KEY_SCREEN_TOP = "shared_element:screenTop"; |
| 126 | protected static final String KEY_SCREEN_RIGHT = "shared_element:screenRight"; |
| 127 | protected static final String KEY_SCREEN_BOTTOM= "shared_element:screenBottom"; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 128 | protected static final String KEY_TRANSLATION_Z = "shared_element:translationZ"; |
George Mount | 480ca82 | 2014-08-08 16:35:48 -0700 | [diff] [blame] | 129 | protected static final String KEY_SNAPSHOT = "shared_element:bitmap"; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 130 | protected static final String KEY_SCALE_TYPE = "shared_element:scaleType"; |
| 131 | protected static final String KEY_IMAGE_MATRIX = "shared_element:imageMatrix"; |
George Mount | 26c82b6 | 2014-08-08 15:43:59 -0700 | [diff] [blame] | 132 | protected static final String KEY_ELEVATION = "shared_element:elevation"; |
George Mount | 080443b | 2014-05-05 10:47:00 -0700 | [diff] [blame] | 133 | |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 134 | protected static final ImageView.ScaleType[] SCALE_TYPE_VALUES = ImageView.ScaleType.values(); |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 135 | |
| 136 | /** |
| 137 | * Sent by the exiting coordinator (either EnterTransitionCoordinator |
| 138 | * or ExitTransitionCoordinator) after the shared elements have |
| 139 | * become stationary (shared element transition completes). This tells |
| 140 | * the remote coordinator to take control of the shared elements and |
| 141 | * that animations may begin. The remote Activity won't start entering |
| 142 | * until this message is received, but may wait for |
| 143 | * MSG_EXIT_TRANSITION_COMPLETE if allowOverlappingTransitions() is true. |
| 144 | */ |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 145 | public static final int MSG_SET_REMOTE_RECEIVER = 100; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 146 | |
| 147 | /** |
| 148 | * Sent by the entering coordinator to tell the exiting coordinator |
| 149 | * to hide its shared elements after it has started its shared |
| 150 | * element transition. This is temporary until the |
| 151 | * interlock of shared elements is figured out. |
| 152 | */ |
| 153 | public static final int MSG_HIDE_SHARED_ELEMENTS = 101; |
| 154 | |
| 155 | /** |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 156 | * Sent by the exiting coordinator (either EnterTransitionCoordinator |
| 157 | * or ExitTransitionCoordinator) after the shared elements have |
| 158 | * become stationary (shared element transition completes). This tells |
| 159 | * the remote coordinator to take control of the shared elements and |
| 160 | * that animations may begin. The remote Activity won't start entering |
| 161 | * until this message is received, but may wait for |
| 162 | * MSG_EXIT_TRANSITION_COMPLETE if allowOverlappingTransitions() is true. |
| 163 | */ |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 164 | public static final int MSG_TAKE_SHARED_ELEMENTS = 103; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 165 | |
| 166 | /** |
| 167 | * Sent by the exiting coordinator (either |
| 168 | * EnterTransitionCoordinator or ExitTransitionCoordinator) after |
| 169 | * the exiting Views have finished leaving the scene. This will |
| 170 | * be ignored if allowOverlappingTransitions() is true on the |
| 171 | * remote coordinator. If it is false, it will trigger the enter |
| 172 | * transition to start. |
| 173 | */ |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 174 | public static final int MSG_EXIT_TRANSITION_COMPLETE = 104; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 175 | |
| 176 | /** |
| 177 | * Sent by Activity#startActivity to begin the exit transition. |
| 178 | */ |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 179 | public static final int MSG_START_EXIT_TRANSITION = 105; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 180 | |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 181 | /** |
| 182 | * It took too long for a message from the entering Activity, so we canceled the transition. |
| 183 | */ |
| 184 | public static final int MSG_CANCEL = 106; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 185 | |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 186 | /** |
| 187 | * When returning, this is the destination location for the shared element. |
| 188 | */ |
| 189 | public static final int MSG_SHARED_ELEMENT_DESTINATION = 107; |
| 190 | |
George Mount | a0a0260 | 2014-06-20 18:22:26 -0700 | [diff] [blame] | 191 | private Window mWindow; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 192 | final protected ArrayList<String> mAllSharedElementNames; |
| 193 | final protected ArrayList<View> mSharedElements = new ArrayList<View>(); |
| 194 | final protected ArrayList<String> mSharedElementNames = new ArrayList<String>(); |
George Mount | b694e08 | 2014-09-12 07:34:52 -0700 | [diff] [blame] | 195 | protected ArrayList<View> mTransitioningViews = new ArrayList<View>(); |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 196 | protected SharedElementCallback mListener; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 197 | protected ResultReceiver mResultReceiver; |
| 198 | final private FixedEpicenterCallback mEpicenterCallback = new FixedEpicenterCallback(); |
George Mount | c9b6df8 | 2014-05-21 15:07:04 -0700 | [diff] [blame] | 199 | final protected boolean mIsReturning; |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 200 | private Runnable mPendingTransition; |
| 201 | private boolean mIsStartingTransition; |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 202 | private ArrayList<GhostViewListeners> mGhostViewListeners = |
| 203 | new ArrayList<GhostViewListeners>(); |
George Mount | 0f0c473 | 2014-09-05 13:47:47 -0700 | [diff] [blame] | 204 | private ArrayMap<View, Float> mOriginalAlphas = new ArrayMap<View, Float>(); |
George Mount | 333b809 | 2014-10-21 15:09:11 -0700 | [diff] [blame] | 205 | private ArrayList<Matrix> mSharedElementParentMatrices; |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 206 | private boolean mSharedElementTransitionComplete; |
| 207 | private boolean mViewsTransitionComplete; |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 208 | |
George Mount | 8c2614c | 2014-06-10 11:12:01 -0700 | [diff] [blame] | 209 | public ActivityTransitionCoordinator(Window window, |
| 210 | ArrayList<String> allSharedElementNames, |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 211 | SharedElementCallback listener, boolean isReturning) { |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 212 | super(new Handler()); |
| 213 | mWindow = window; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 214 | mListener = listener; |
| 215 | mAllSharedElementNames = allSharedElementNames; |
George Mount | c9b6df8 | 2014-05-21 15:07:04 -0700 | [diff] [blame] | 216 | mIsReturning = isReturning; |
George Mount | 8c2614c | 2014-06-10 11:12:01 -0700 | [diff] [blame] | 217 | } |
| 218 | |
George Mount | 1fecfb2 | 2014-06-18 14:55:55 -0700 | [diff] [blame] | 219 | protected void viewsReady(ArrayMap<String, View> sharedElements) { |
George Mount | ce725a4 | 2014-08-27 16:10:46 -0700 | [diff] [blame] | 220 | sharedElements.retainAll(mAllSharedElementNames); |
George Mount | 1732f52 | 2014-09-17 16:59:36 -0700 | [diff] [blame] | 221 | if (mListener != null) { |
| 222 | mListener.onMapSharedElements(mAllSharedElementNames, sharedElements); |
| 223 | } |
George Mount | 333b809 | 2014-10-21 15:09:11 -0700 | [diff] [blame] | 224 | setSharedElements(sharedElements); |
George Mount | b694e08 | 2014-09-12 07:34:52 -0700 | [diff] [blame] | 225 | if (getViewsTransition() != null && mTransitioningViews != null) { |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 226 | ViewGroup decorView = getDecor(); |
| 227 | if (decorView != null) { |
| 228 | decorView.captureTransitioningViews(mTransitioningViews); |
| 229 | } |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 230 | mTransitioningViews.removeAll(mSharedElements); |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 231 | } |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 232 | setEpicenter(); |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 233 | } |
| 234 | |
George Mount | 333b809 | 2014-10-21 15:09:11 -0700 | [diff] [blame] | 235 | /** |
| 236 | * Iterates over the shared elements and adds them to the members in order. |
| 237 | * Shared elements that are nested in other shared elements are placed after the |
| 238 | * elements that they are nested in. This means that layout ordering can be done |
| 239 | * from first to last. |
| 240 | * |
| 241 | * @param sharedElements The map of transition names to shared elements to set into |
| 242 | * the member fields. |
| 243 | */ |
| 244 | private void setSharedElements(ArrayMap<String, View> sharedElements) { |
| 245 | boolean isFirstRun = true; |
| 246 | while (!sharedElements.isEmpty()) { |
| 247 | final int numSharedElements = sharedElements.size(); |
| 248 | for (int i = numSharedElements - 1; i >= 0; i--) { |
| 249 | final View view = sharedElements.valueAt(i); |
| 250 | final String name = sharedElements.keyAt(i); |
| 251 | if (isFirstRun && (view == null || !view.isAttachedToWindow() || name == null)) { |
| 252 | sharedElements.removeAt(i); |
George Mount | 42161c5 | 2015-02-24 16:18:09 -0800 | [diff] [blame] | 253 | } else if (!isNested(view, sharedElements)) { |
| 254 | mSharedElementNames.add(name); |
| 255 | mSharedElements.add(view); |
| 256 | sharedElements.removeAt(i); |
George Mount | 333b809 | 2014-10-21 15:09:11 -0700 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | isFirstRun = false; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Returns true when view is nested in any of the values of sharedElements. |
| 265 | */ |
| 266 | private static boolean isNested(View view, ArrayMap<String, View> sharedElements) { |
| 267 | ViewParent parent = view.getParent(); |
| 268 | boolean isNested = false; |
| 269 | while (parent instanceof View) { |
| 270 | View parentView = (View) parent; |
| 271 | if (sharedElements.containsValue(parentView)) { |
| 272 | isNested = true; |
| 273 | break; |
| 274 | } |
| 275 | parent = parentView.getParent(); |
| 276 | } |
| 277 | return isNested; |
| 278 | } |
| 279 | |
George Mount | 60625b0 | 2014-06-24 07:46:23 -0700 | [diff] [blame] | 280 | protected void stripOffscreenViews() { |
George Mount | b694e08 | 2014-09-12 07:34:52 -0700 | [diff] [blame] | 281 | if (mTransitioningViews == null) { |
| 282 | return; |
| 283 | } |
George Mount | 60625b0 | 2014-06-24 07:46:23 -0700 | [diff] [blame] | 284 | Rect r = new Rect(); |
| 285 | for (int i = mTransitioningViews.size() - 1; i >= 0; i--) { |
| 286 | View view = mTransitioningViews.get(i); |
| 287 | if (!view.getGlobalVisibleRect(r)) { |
| 288 | mTransitioningViews.remove(i); |
George Mount | 653ea66 | 2014-09-12 13:56:32 -0700 | [diff] [blame] | 289 | showView(view, true); |
George Mount | 60625b0 | 2014-06-24 07:46:23 -0700 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 294 | protected Window getWindow() { |
| 295 | return mWindow; |
| 296 | } |
| 297 | |
George Mount | a2bbbb3 | 2014-08-12 10:16:20 -0700 | [diff] [blame] | 298 | public ViewGroup getDecor() { |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 299 | return (mWindow == null) ? null : (ViewGroup) mWindow.getDecorView(); |
| 300 | } |
| 301 | |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 302 | /** |
| 303 | * Sets the transition epicenter to the position of the first shared element. |
| 304 | */ |
| 305 | protected void setEpicenter() { |
| 306 | View epicenter = null; |
George Mount | c6186bf | 2014-09-04 16:33:59 -0700 | [diff] [blame] | 307 | if (!mAllSharedElementNames.isEmpty() && !mSharedElementNames.isEmpty()) { |
| 308 | int index = mSharedElementNames.indexOf(mAllSharedElementNames.get(0)); |
| 309 | if (index >= 0) { |
| 310 | epicenter = mSharedElements.get(index); |
| 311 | } |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 312 | } |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 313 | setEpicenter(epicenter); |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 314 | } |
| 315 | |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 316 | private void setEpicenter(View view) { |
| 317 | if (view == null) { |
| 318 | mEpicenterCallback.setEpicenter(null); |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 319 | } else { |
George Mount | 8e43d6d | 2014-06-05 17:25:46 -0700 | [diff] [blame] | 320 | Rect epicenter = new Rect(); |
| 321 | view.getBoundsOnScreen(epicenter); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 322 | mEpicenterCallback.setEpicenter(epicenter); |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 326 | public ArrayList<String> getAcceptedNames() { |
| 327 | return mSharedElementNames; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 328 | } |
| 329 | |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 330 | public ArrayList<String> getMappedNames() { |
| 331 | ArrayList<String> names = new ArrayList<String>(mSharedElements.size()); |
| 332 | for (int i = 0; i < mSharedElements.size(); i++) { |
George Mount | 0a2ae00 | 2014-06-23 14:57:27 +0000 | [diff] [blame] | 333 | names.add(mSharedElements.get(i).getTransitionName()); |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 334 | } |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 335 | return names; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 336 | } |
| 337 | |
George Mount | 700db2a | 2014-07-07 17:17:49 -0700 | [diff] [blame] | 338 | public ArrayList<View> copyMappedViews() { |
| 339 | return new ArrayList<View>(mSharedElements); |
George Mount | 1fecfb2 | 2014-06-18 14:55:55 -0700 | [diff] [blame] | 340 | } |
| 341 | |
George Mount | 8c2614c | 2014-06-10 11:12:01 -0700 | [diff] [blame] | 342 | public ArrayList<String> getAllSharedElementNames() { return mAllSharedElementNames; } |
| 343 | |
George Mount | 8881502 | 2014-06-25 14:33:54 -0700 | [diff] [blame] | 344 | protected Transition setTargets(Transition transition, boolean add) { |
| 345 | if (transition == null || (add && |
| 346 | (mTransitioningViews == null || mTransitioningViews.isEmpty()))) { |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 347 | return null; |
| 348 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 349 | // Add the targets to a set containing transition so that transition |
| 350 | // remains unaffected. We don't want to modify the targets of transition itself. |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 351 | TransitionSet set = new TransitionSet(); |
George Mount | 8881502 | 2014-06-25 14:33:54 -0700 | [diff] [blame] | 352 | if (mTransitioningViews != null) { |
| 353 | for (int i = mTransitioningViews.size() - 1; i >= 0; i--) { |
| 354 | View view = mTransitioningViews.get(i); |
| 355 | if (add) { |
| 356 | set.addTarget(view); |
| 357 | } else { |
| 358 | set.excludeTarget(view, true); |
| 359 | } |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 360 | } |
| 361 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 362 | // By adding the transition after addTarget, we prevent addTarget from |
| 363 | // affecting transition. |
| 364 | set.addTransition(transition); |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 365 | |
| 366 | if (!add && mTransitioningViews != null && !mTransitioningViews.isEmpty()) { |
| 367 | // Allow children of excluded transitioning views, but not the views themselves |
| 368 | set = new TransitionSet().addTransition(set); |
| 369 | } |
| 370 | |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 371 | return set; |
| 372 | } |
| 373 | |
George Mount | 8881502 | 2014-06-25 14:33:54 -0700 | [diff] [blame] | 374 | protected Transition configureTransition(Transition transition, |
| 375 | boolean includeTransitioningViews) { |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 376 | if (transition != null) { |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 377 | transition = transition.clone(); |
| 378 | transition.setEpicenterCallback(mEpicenterCallback); |
George Mount | 8881502 | 2014-06-25 14:33:54 -0700 | [diff] [blame] | 379 | transition = setTargets(transition, includeTransitioningViews); |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 380 | } |
| 381 | return transition; |
| 382 | } |
| 383 | |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 384 | protected static Transition mergeTransitions(Transition transition1, Transition transition2) { |
| 385 | if (transition1 == null) { |
| 386 | return transition2; |
| 387 | } else if (transition2 == null) { |
| 388 | return transition1; |
| 389 | } else { |
| 390 | TransitionSet transitionSet = new TransitionSet(); |
| 391 | transitionSet.addTransition(transition1); |
| 392 | transitionSet.addTransition(transition2); |
| 393 | return transitionSet; |
George Mount | caa0310 | 2014-04-15 09:01:32 -0700 | [diff] [blame] | 394 | } |
George Mount | caa0310 | 2014-04-15 09:01:32 -0700 | [diff] [blame] | 395 | } |
| 396 | |
George Mount | 1fecfb2 | 2014-06-18 14:55:55 -0700 | [diff] [blame] | 397 | protected ArrayMap<String, View> mapSharedElements(ArrayList<String> accepted, |
| 398 | ArrayList<View> localViews) { |
| 399 | ArrayMap<String, View> sharedElements = new ArrayMap<String, View>(); |
George Mount | ce725a4 | 2014-08-27 16:10:46 -0700 | [diff] [blame] | 400 | if (accepted != null) { |
| 401 | for (int i = 0; i < accepted.size(); i++) { |
| 402 | sharedElements.put(accepted.get(i), localViews.get(i)); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 403 | } |
George Mount | ce725a4 | 2014-08-27 16:10:46 -0700 | [diff] [blame] | 404 | } else { |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 405 | ViewGroup decorView = getDecor(); |
| 406 | if (decorView != null) { |
| 407 | decorView.findNamedViews(sharedElements); |
| 408 | } |
George Mount | 1fecfb2 | 2014-06-18 14:55:55 -0700 | [diff] [blame] | 409 | } |
| 410 | return sharedElements; |
| 411 | } |
| 412 | |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 413 | protected void setResultReceiver(ResultReceiver resultReceiver) { |
| 414 | mResultReceiver = resultReceiver; |
George Mount | 080443b | 2014-05-05 10:47:00 -0700 | [diff] [blame] | 415 | } |
| 416 | |
George Mount | a712e8c | 2014-05-20 15:10:20 -0700 | [diff] [blame] | 417 | protected abstract Transition getViewsTransition(); |
George Mount | 080443b | 2014-05-05 10:47:00 -0700 | [diff] [blame] | 418 | |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 419 | private void setSharedElementState(View view, String name, Bundle transitionArgs, |
Dake Gu | 7bf379c | 2014-07-15 16:29:38 -0700 | [diff] [blame] | 420 | Matrix tempMatrix, RectF tempRect, int[] decorLoc) { |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 421 | Bundle sharedElementBundle = transitionArgs.getBundle(name); |
| 422 | if (sharedElementBundle == null) { |
| 423 | return; |
| 424 | } |
| 425 | |
| 426 | if (view instanceof ImageView) { |
| 427 | int scaleTypeInt = sharedElementBundle.getInt(KEY_SCALE_TYPE, -1); |
| 428 | if (scaleTypeInt >= 0) { |
| 429 | ImageView imageView = (ImageView) view; |
| 430 | ImageView.ScaleType scaleType = SCALE_TYPE_VALUES[scaleTypeInt]; |
| 431 | imageView.setScaleType(scaleType); |
| 432 | if (scaleType == ImageView.ScaleType.MATRIX) { |
| 433 | float[] matrixValues = sharedElementBundle.getFloatArray(KEY_IMAGE_MATRIX); |
Dake Gu | 7bf379c | 2014-07-15 16:29:38 -0700 | [diff] [blame] | 434 | tempMatrix.setValues(matrixValues); |
| 435 | imageView.setImageMatrix(tempMatrix); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | float z = sharedElementBundle.getFloat(KEY_TRANSLATION_Z); |
| 441 | view.setTranslationZ(z); |
George Mount | 26c82b6 | 2014-08-08 15:43:59 -0700 | [diff] [blame] | 442 | float elevation = sharedElementBundle.getFloat(KEY_ELEVATION); |
| 443 | view.setElevation(elevation); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 444 | |
Dake Gu | 7bf379c | 2014-07-15 16:29:38 -0700 | [diff] [blame] | 445 | float left = sharedElementBundle.getFloat(KEY_SCREEN_LEFT); |
| 446 | float top = sharedElementBundle.getFloat(KEY_SCREEN_TOP); |
| 447 | float right = sharedElementBundle.getFloat(KEY_SCREEN_RIGHT); |
| 448 | float bottom = sharedElementBundle.getFloat(KEY_SCREEN_BOTTOM); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 449 | |
Dake Gu | 7bf379c | 2014-07-15 16:29:38 -0700 | [diff] [blame] | 450 | if (decorLoc != null) { |
| 451 | left -= decorLoc[0]; |
| 452 | top -= decorLoc[1]; |
George Mount | 480ca82 | 2014-08-08 16:35:48 -0700 | [diff] [blame] | 453 | right -= decorLoc[0]; |
| 454 | bottom -= decorLoc[1]; |
Dake Gu | 7bf379c | 2014-07-15 16:29:38 -0700 | [diff] [blame] | 455 | } else { |
| 456 | // Find the location in the view's parent |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 457 | getSharedElementParentMatrix(view, tempMatrix); |
Dake Gu | 7bf379c | 2014-07-15 16:29:38 -0700 | [diff] [blame] | 458 | tempRect.set(left, top, right, bottom); |
| 459 | tempMatrix.mapRect(tempRect); |
| 460 | |
| 461 | float leftInParent = tempRect.left; |
| 462 | float topInParent = tempRect.top; |
| 463 | |
| 464 | // Find the size of the view |
| 465 | view.getInverseMatrix().mapRect(tempRect); |
| 466 | float width = tempRect.width(); |
| 467 | float height = tempRect.height(); |
| 468 | |
| 469 | // Now determine the offset due to view transform: |
| 470 | view.setLeft(0); |
| 471 | view.setTop(0); |
| 472 | view.setRight(Math.round(width)); |
| 473 | view.setBottom(Math.round(height)); |
| 474 | tempRect.set(0, 0, width, height); |
| 475 | view.getMatrix().mapRect(tempRect); |
| 476 | |
Doris Liu | 18c2b0e | 2015-05-01 11:02:01 -0700 | [diff] [blame] | 477 | left = leftInParent - tempRect.left; |
| 478 | top = topInParent - tempRect.top; |
Dake Gu | 7bf379c | 2014-07-15 16:29:38 -0700 | [diff] [blame] | 479 | right = left + width; |
| 480 | bottom = top + height; |
| 481 | } |
| 482 | |
| 483 | int x = Math.round(left); |
| 484 | int y = Math.round(top); |
| 485 | int width = Math.round(right) - x; |
| 486 | int height = Math.round(bottom) - y; |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 487 | int widthSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY); |
| 488 | int heightSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY); |
| 489 | view.measure(widthSpec, heightSpec); |
| 490 | |
Dake Gu | 7bf379c | 2014-07-15 16:29:38 -0700 | [diff] [blame] | 491 | view.layout(x, y, x + width, y + height); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 492 | } |
| 493 | |
George Mount | 333b809 | 2014-10-21 15:09:11 -0700 | [diff] [blame] | 494 | private void setSharedElementMatrices() { |
| 495 | int numSharedElements = mSharedElements.size(); |
| 496 | if (numSharedElements > 0) { |
| 497 | mSharedElementParentMatrices = new ArrayList<Matrix>(numSharedElements); |
| 498 | } |
| 499 | for (int i = 0; i < numSharedElements; i++) { |
| 500 | View view = mSharedElements.get(i); |
| 501 | |
| 502 | // Find the location in the view's parent |
| 503 | ViewGroup parent = (ViewGroup) view.getParent(); |
| 504 | Matrix matrix = new Matrix(); |
| 505 | parent.transformMatrixToLocal(matrix); |
Doris Liu | 18c2b0e | 2015-05-01 11:02:01 -0700 | [diff] [blame] | 506 | matrix.postTranslate(parent.getScrollX(), parent.getScrollY()); |
George Mount | 333b809 | 2014-10-21 15:09:11 -0700 | [diff] [blame] | 507 | mSharedElementParentMatrices.add(matrix); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | private void getSharedElementParentMatrix(View view, Matrix matrix) { |
George Mount | 42161c5 | 2015-02-24 16:18:09 -0800 | [diff] [blame] | 512 | final int index = mSharedElementParentMatrices == null ? -1 |
| 513 | : mSharedElements.indexOf(view); |
| 514 | if (index < 0) { |
George Mount | 333b809 | 2014-10-21 15:09:11 -0700 | [diff] [blame] | 515 | matrix.reset(); |
| 516 | ViewParent viewParent = view.getParent(); |
| 517 | if (viewParent instanceof ViewGroup) { |
| 518 | // Find the location in the view's parent |
| 519 | ViewGroup parent = (ViewGroup) viewParent; |
| 520 | parent.transformMatrixToLocal(matrix); |
Doris Liu | 18c2b0e | 2015-05-01 11:02:01 -0700 | [diff] [blame] | 521 | matrix.postTranslate(parent.getScrollX(), parent.getScrollY()); |
George Mount | 333b809 | 2014-10-21 15:09:11 -0700 | [diff] [blame] | 522 | } |
George Mount | 42161c5 | 2015-02-24 16:18:09 -0800 | [diff] [blame] | 523 | } else { |
| 524 | // The indices of mSharedElementParentMatrices matches the |
| 525 | // mSharedElement matrices. |
| 526 | Matrix parentMatrix = mSharedElementParentMatrices.get(index); |
| 527 | matrix.set(parentMatrix); |
George Mount | 333b809 | 2014-10-21 15:09:11 -0700 | [diff] [blame] | 528 | } |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Dake Gu | c18f4cc | 2014-07-11 17:48:37 -0700 | [diff] [blame] | 531 | protected ArrayList<SharedElementOriginalState> setSharedElementState( |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 532 | Bundle sharedElementState, final ArrayList<View> snapshots) { |
Dake Gu | c18f4cc | 2014-07-11 17:48:37 -0700 | [diff] [blame] | 533 | ArrayList<SharedElementOriginalState> originalImageState = |
| 534 | new ArrayList<SharedElementOriginalState>(); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 535 | if (sharedElementState != null) { |
Dake Gu | 7bf379c | 2014-07-15 16:29:38 -0700 | [diff] [blame] | 536 | Matrix tempMatrix = new Matrix(); |
| 537 | RectF tempRect = new RectF(); |
George Mount | 1732f52 | 2014-09-17 16:59:36 -0700 | [diff] [blame] | 538 | final int numSharedElements = mSharedElements.size(); |
| 539 | for (int i = 0; i < numSharedElements; i++) { |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 540 | View sharedElement = mSharedElements.get(i); |
| 541 | String name = mSharedElementNames.get(i); |
Dake Gu | c18f4cc | 2014-07-11 17:48:37 -0700 | [diff] [blame] | 542 | SharedElementOriginalState originalState = getOldSharedElementState(sharedElement, |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 543 | name, sharedElementState); |
Dake Gu | c18f4cc | 2014-07-11 17:48:37 -0700 | [diff] [blame] | 544 | originalImageState.add(originalState); |
Dake Gu | 7bf379c | 2014-07-15 16:29:38 -0700 | [diff] [blame] | 545 | setSharedElementState(sharedElement, name, sharedElementState, |
| 546 | tempMatrix, tempRect, null); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 547 | } |
| 548 | } |
George Mount | 1732f52 | 2014-09-17 16:59:36 -0700 | [diff] [blame] | 549 | if (mListener != null) { |
| 550 | mListener.onSharedElementStart(mSharedElementNames, mSharedElements, snapshots); |
| 551 | } |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 552 | return originalImageState; |
| 553 | } |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 554 | |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 555 | protected void notifySharedElementEnd(ArrayList<View> snapshots) { |
George Mount | 1732f52 | 2014-09-17 16:59:36 -0700 | [diff] [blame] | 556 | if (mListener != null) { |
| 557 | mListener.onSharedElementEnd(mSharedElementNames, mSharedElements, snapshots); |
| 558 | } |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | protected void scheduleSetSharedElementEnd(final ArrayList<View> snapshots) { |
George Mount | 6e7fb60 | 2014-09-04 16:20:20 -0700 | [diff] [blame] | 562 | final View decorView = getDecor(); |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 563 | if (decorView != null) { |
| 564 | decorView.getViewTreeObserver().addOnPreDrawListener( |
| 565 | new ViewTreeObserver.OnPreDrawListener() { |
| 566 | @Override |
| 567 | public boolean onPreDraw() { |
| 568 | decorView.getViewTreeObserver().removeOnPreDrawListener(this); |
| 569 | notifySharedElementEnd(snapshots); |
| 570 | return true; |
| 571 | } |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 572 | } |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 573 | ); |
| 574 | } |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Dake Gu | c18f4cc | 2014-07-11 17:48:37 -0700 | [diff] [blame] | 577 | private static SharedElementOriginalState getOldSharedElementState(View view, String name, |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 578 | Bundle transitionArgs) { |
Dake Gu | c18f4cc | 2014-07-11 17:48:37 -0700 | [diff] [blame] | 579 | |
| 580 | SharedElementOriginalState state = new SharedElementOriginalState(); |
| 581 | state.mLeft = view.getLeft(); |
| 582 | state.mTop = view.getTop(); |
| 583 | state.mRight = view.getRight(); |
| 584 | state.mBottom = view.getBottom(); |
| 585 | state.mMeasuredWidth = view.getMeasuredWidth(); |
| 586 | state.mMeasuredHeight = view.getMeasuredHeight(); |
George Mount | 26c82b6 | 2014-08-08 15:43:59 -0700 | [diff] [blame] | 587 | state.mTranslationZ = view.getTranslationZ(); |
| 588 | state.mElevation = view.getElevation(); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 589 | if (!(view instanceof ImageView)) { |
Dake Gu | c18f4cc | 2014-07-11 17:48:37 -0700 | [diff] [blame] | 590 | return state; |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 591 | } |
| 592 | Bundle bundle = transitionArgs.getBundle(name); |
| 593 | if (bundle == null) { |
Dake Gu | c18f4cc | 2014-07-11 17:48:37 -0700 | [diff] [blame] | 594 | return state; |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 595 | } |
| 596 | int scaleTypeInt = bundle.getInt(KEY_SCALE_TYPE, -1); |
| 597 | if (scaleTypeInt < 0) { |
Dake Gu | c18f4cc | 2014-07-11 17:48:37 -0700 | [diff] [blame] | 598 | return state; |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | ImageView imageView = (ImageView) view; |
Dake Gu | c18f4cc | 2014-07-11 17:48:37 -0700 | [diff] [blame] | 602 | state.mScaleType = imageView.getScaleType(); |
| 603 | if (state.mScaleType == ImageView.ScaleType.MATRIX) { |
| 604 | state.mMatrix = new Matrix(imageView.getImageMatrix()); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 605 | } |
Dake Gu | c18f4cc | 2014-07-11 17:48:37 -0700 | [diff] [blame] | 606 | return state; |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | protected ArrayList<View> createSnapshots(Bundle state, Collection<String> names) { |
| 610 | int numSharedElements = names.size(); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 611 | ArrayList<View> snapshots = new ArrayList<View>(numSharedElements); |
George Mount | 7c479f5 | 2014-10-15 16:21:51 -0700 | [diff] [blame] | 612 | if (numSharedElements == 0) { |
| 613 | return snapshots; |
| 614 | } |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 615 | Context context = getWindow().getContext(); |
Dake Gu | 7bf379c | 2014-07-15 16:29:38 -0700 | [diff] [blame] | 616 | int[] decorLoc = new int[2]; |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 617 | ViewGroup decorView = getDecor(); |
| 618 | if (decorView != null) { |
| 619 | decorView.getLocationOnScreen(decorLoc); |
| 620 | } |
George Mount | ca5094a | 2014-10-30 11:28:00 -0700 | [diff] [blame] | 621 | Matrix tempMatrix = new Matrix(); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 622 | for (String name: names) { |
| 623 | Bundle sharedElementBundle = state.getBundle(name); |
George Mount | 92692c0 | 2014-12-01 16:44:05 -0800 | [diff] [blame] | 624 | View snapshot = null; |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 625 | if (sharedElementBundle != null) { |
George Mount | 480ca82 | 2014-08-08 16:35:48 -0700 | [diff] [blame] | 626 | Parcelable parcelable = sharedElementBundle.getParcelable(KEY_SNAPSHOT); |
George Mount | 1732f52 | 2014-09-17 16:59:36 -0700 | [diff] [blame] | 627 | if (parcelable != null && mListener != null) { |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 628 | snapshot = mListener.onCreateSnapshotView(context, parcelable); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 629 | } |
George Mount | 480ca82 | 2014-08-08 16:35:48 -0700 | [diff] [blame] | 630 | if (snapshot != null) { |
George Mount | ca5094a | 2014-10-30 11:28:00 -0700 | [diff] [blame] | 631 | setSharedElementState(snapshot, name, state, tempMatrix, null, decorLoc); |
George Mount | 480ca82 | 2014-08-08 16:35:48 -0700 | [diff] [blame] | 632 | } |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 633 | } |
George Mount | 92692c0 | 2014-12-01 16:44:05 -0800 | [diff] [blame] | 634 | // Even null snapshots are added so they remain in the same order as shared elements. |
| 635 | snapshots.add(snapshot); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 636 | } |
| 637 | return snapshots; |
| 638 | } |
| 639 | |
Dake Gu | c18f4cc | 2014-07-11 17:48:37 -0700 | [diff] [blame] | 640 | protected static void setOriginalSharedElementState(ArrayList<View> sharedElements, |
| 641 | ArrayList<SharedElementOriginalState> originalState) { |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 642 | for (int i = 0; i < originalState.size(); i++) { |
Dake Gu | c18f4cc | 2014-07-11 17:48:37 -0700 | [diff] [blame] | 643 | View view = sharedElements.get(i); |
| 644 | SharedElementOriginalState state = originalState.get(i); |
| 645 | if (view instanceof ImageView && state.mScaleType != null) { |
| 646 | ImageView imageView = (ImageView) view; |
| 647 | imageView.setScaleType(state.mScaleType); |
| 648 | if (state.mScaleType == ImageView.ScaleType.MATRIX) { |
| 649 | imageView.setImageMatrix(state.mMatrix); |
| 650 | } |
| 651 | } |
George Mount | 26c82b6 | 2014-08-08 15:43:59 -0700 | [diff] [blame] | 652 | view.setElevation(state.mElevation); |
| 653 | view.setTranslationZ(state.mTranslationZ); |
| 654 | int widthSpec = View.MeasureSpec.makeMeasureSpec(state.mMeasuredWidth, |
Dake Gu | c18f4cc | 2014-07-11 17:48:37 -0700 | [diff] [blame] | 655 | View.MeasureSpec.EXACTLY); |
| 656 | int heightSpec = View.MeasureSpec.makeMeasureSpec(state.mMeasuredHeight, |
| 657 | View.MeasureSpec.EXACTLY); |
| 658 | view.measure(widthSpec, heightSpec); |
| 659 | view.layout(state.mLeft, state.mTop, state.mRight, state.mBottom); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | |
| 663 | protected Bundle captureSharedElementState() { |
| 664 | Bundle bundle = new Bundle(); |
Dake Gu | 7bf379c | 2014-07-15 16:29:38 -0700 | [diff] [blame] | 665 | RectF tempBounds = new RectF(); |
| 666 | Matrix tempMatrix = new Matrix(); |
Dake Gu | f03c139 | 2014-07-25 15:36:23 -0700 | [diff] [blame] | 667 | for (int i = 0; i < mSharedElements.size(); i++) { |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 668 | View sharedElement = mSharedElements.get(i); |
| 669 | String name = mSharedElementNames.get(i); |
Dake Gu | 7bf379c | 2014-07-15 16:29:38 -0700 | [diff] [blame] | 670 | captureSharedElementState(sharedElement, name, bundle, tempMatrix, tempBounds); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 671 | } |
| 672 | return bundle; |
| 673 | } |
| 674 | |
George Mount | a0a0260 | 2014-06-20 18:22:26 -0700 | [diff] [blame] | 675 | protected void clearState() { |
| 676 | // Clear the state so that we can't hold any references accidentally and leak memory. |
| 677 | mWindow = null; |
George Mount | a0a0260 | 2014-06-20 18:22:26 -0700 | [diff] [blame] | 678 | mSharedElements.clear(); |
George Mount | b694e08 | 2014-09-12 07:34:52 -0700 | [diff] [blame] | 679 | mTransitioningViews = null; |
George Mount | 0f0c473 | 2014-09-05 13:47:47 -0700 | [diff] [blame] | 680 | mOriginalAlphas.clear(); |
George Mount | a0a0260 | 2014-06-20 18:22:26 -0700 | [diff] [blame] | 681 | mResultReceiver = null; |
| 682 | mPendingTransition = null; |
Dake Gu | fc0fc0e | 2014-08-01 15:39:11 -0700 | [diff] [blame] | 683 | mListener = null; |
George Mount | 333b809 | 2014-10-21 15:09:11 -0700 | [diff] [blame] | 684 | mSharedElementParentMatrices = null; |
George Mount | a0a0260 | 2014-06-20 18:22:26 -0700 | [diff] [blame] | 685 | } |
| 686 | |
George Mount | ed1e01d | 2014-06-05 13:49:12 -0700 | [diff] [blame] | 687 | protected long getFadeDuration() { |
| 688 | return getWindow().getTransitionBackgroundFadeDuration(); |
| 689 | } |
| 690 | |
George Mount | 0f0c473 | 2014-09-05 13:47:47 -0700 | [diff] [blame] | 691 | protected void hideViews(ArrayList<View> views) { |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 692 | int count = views.size(); |
| 693 | for (int i = 0; i < count; i++) { |
George Mount | 0f0c473 | 2014-09-05 13:47:47 -0700 | [diff] [blame] | 694 | View view = views.get(i); |
| 695 | if (!mOriginalAlphas.containsKey(view)) { |
| 696 | mOriginalAlphas.put(view, view.getAlpha()); |
| 697 | } |
| 698 | view.setAlpha(0f); |
George Mount | 0f0c473 | 2014-09-05 13:47:47 -0700 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | |
George Mount | ce2ee3d | 2014-09-09 15:04:31 -0700 | [diff] [blame] | 702 | protected void showViews(ArrayList<View> views, boolean setTransitionAlpha) { |
George Mount | 0f0c473 | 2014-09-05 13:47:47 -0700 | [diff] [blame] | 703 | int count = views.size(); |
| 704 | for (int i = 0; i < count; i++) { |
George Mount | 653ea66 | 2014-09-12 13:56:32 -0700 | [diff] [blame] | 705 | showView(views.get(i), setTransitionAlpha); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | private void showView(View view, boolean setTransitionAlpha) { |
| 710 | Float alpha = mOriginalAlphas.remove(view); |
| 711 | if (alpha != null) { |
| 712 | view.setAlpha(alpha); |
| 713 | } |
| 714 | if (setTransitionAlpha) { |
| 715 | view.setTransitionAlpha(1f); |
George Mount | b5ef7f8 | 2014-07-09 14:55:03 -0700 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 719 | /** |
| 720 | * Captures placement information for Views with a shared element name for |
| 721 | * Activity Transitions. |
| 722 | * |
| 723 | * @param view The View to capture the placement information for. |
| 724 | * @param name The shared element name in the target Activity to apply the placement |
| 725 | * information for. |
| 726 | * @param transitionArgs Bundle to store shared element placement information. |
George Mount | 8e43d6d | 2014-06-05 17:25:46 -0700 | [diff] [blame] | 727 | * @param tempBounds A temporary Rect for capturing the current location of views. |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 728 | */ |
George Mount | 480ca82 | 2014-08-08 16:35:48 -0700 | [diff] [blame] | 729 | protected void captureSharedElementState(View view, String name, Bundle transitionArgs, |
Dake Gu | 7bf379c | 2014-07-15 16:29:38 -0700 | [diff] [blame] | 730 | Matrix tempMatrix, RectF tempBounds) { |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 731 | Bundle sharedElementBundle = new Bundle(); |
Dake Gu | 7bf379c | 2014-07-15 16:29:38 -0700 | [diff] [blame] | 732 | tempMatrix.reset(); |
| 733 | view.transformMatrixToGlobal(tempMatrix); |
George Mount | 8e43d6d | 2014-06-05 17:25:46 -0700 | [diff] [blame] | 734 | tempBounds.set(0, 0, view.getWidth(), view.getHeight()); |
Dake Gu | 7bf379c | 2014-07-15 16:29:38 -0700 | [diff] [blame] | 735 | tempMatrix.mapRect(tempBounds); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 736 | |
Dake Gu | 7bf379c | 2014-07-15 16:29:38 -0700 | [diff] [blame] | 737 | sharedElementBundle.putFloat(KEY_SCREEN_LEFT, tempBounds.left); |
| 738 | sharedElementBundle.putFloat(KEY_SCREEN_RIGHT, tempBounds.right); |
| 739 | sharedElementBundle.putFloat(KEY_SCREEN_TOP, tempBounds.top); |
| 740 | sharedElementBundle.putFloat(KEY_SCREEN_BOTTOM, tempBounds.bottom); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 741 | sharedElementBundle.putFloat(KEY_TRANSLATION_Z, view.getTranslationZ()); |
George Mount | 26c82b6 | 2014-08-08 15:43:59 -0700 | [diff] [blame] | 742 | sharedElementBundle.putFloat(KEY_ELEVATION, view.getElevation()); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 743 | |
George Mount | 1732f52 | 2014-09-17 16:59:36 -0700 | [diff] [blame] | 744 | Parcelable bitmap = null; |
| 745 | if (mListener != null) { |
| 746 | bitmap = mListener.onCaptureSharedElementSnapshot(view, tempMatrix, tempBounds); |
| 747 | } |
| 748 | |
George Mount | 480ca82 | 2014-08-08 16:35:48 -0700 | [diff] [blame] | 749 | if (bitmap != null) { |
| 750 | sharedElementBundle.putParcelable(KEY_SNAPSHOT, bitmap); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | if (view instanceof ImageView) { |
| 754 | ImageView imageView = (ImageView) view; |
| 755 | int scaleTypeInt = scaleTypeToInt(imageView.getScaleType()); |
| 756 | sharedElementBundle.putInt(KEY_SCALE_TYPE, scaleTypeInt); |
| 757 | if (imageView.getScaleType() == ImageView.ScaleType.MATRIX) { |
| 758 | float[] matrix = new float[9]; |
| 759 | imageView.getImageMatrix().getValues(matrix); |
| 760 | sharedElementBundle.putFloatArray(KEY_IMAGE_MATRIX, matrix); |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | transitionArgs.putBundle(name, sharedElementBundle); |
| 765 | } |
| 766 | |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 767 | |
| 768 | protected void startTransition(Runnable runnable) { |
| 769 | if (mIsStartingTransition) { |
| 770 | mPendingTransition = runnable; |
| 771 | } else { |
| 772 | mIsStartingTransition = true; |
| 773 | runnable.run(); |
| 774 | } |
| 775 | } |
| 776 | |
George Mount | 13ccb79 | 2014-06-06 17:02:20 -0700 | [diff] [blame] | 777 | protected void transitionStarted() { |
| 778 | mIsStartingTransition = false; |
| 779 | } |
| 780 | |
George Mount | fbd4596 | 2015-01-26 14:38:19 -0800 | [diff] [blame] | 781 | /** |
| 782 | * Cancels any pending transitions and returns true if there is a transition is in |
| 783 | * the middle of starting. |
| 784 | */ |
| 785 | protected boolean cancelPendingTransitions() { |
| 786 | mPendingTransition = null; |
| 787 | return mIsStartingTransition; |
| 788 | } |
| 789 | |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 790 | protected void moveSharedElementsToOverlay() { |
George Mount | eca1ae5 | 2014-10-27 14:25:12 -0700 | [diff] [blame] | 791 | if (mWindow == null || !mWindow.getSharedElementsUseOverlay()) { |
George Mount | b89d5cc | 2014-08-18 16:50:50 -0700 | [diff] [blame] | 792 | return; |
| 793 | } |
George Mount | 333b809 | 2014-10-21 15:09:11 -0700 | [diff] [blame] | 794 | setSharedElementMatrices(); |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 795 | int numSharedElements = mSharedElements.size(); |
| 796 | ViewGroup decor = getDecor(); |
| 797 | if (decor != null) { |
| 798 | boolean moveWithParent = moveSharedElementWithParent(); |
George Mount | 42161c5 | 2015-02-24 16:18:09 -0800 | [diff] [blame] | 799 | Matrix tempMatrix = new Matrix(); |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 800 | for (int i = 0; i < numSharedElements; i++) { |
| 801 | View view = mSharedElements.get(i); |
George Mount | 42161c5 | 2015-02-24 16:18:09 -0800 | [diff] [blame] | 802 | tempMatrix.reset(); |
| 803 | mSharedElementParentMatrices.get(i).invert(tempMatrix); |
| 804 | GhostView.addGhost(view, decor, tempMatrix); |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 805 | ViewGroup parent = (ViewGroup) view.getParent(); |
| 806 | if (moveWithParent && !isInTransitionGroup(parent, decor)) { |
George Mount | 6e7fb60 | 2014-09-04 16:20:20 -0700 | [diff] [blame] | 807 | GhostViewListeners listener = new GhostViewListeners(view, parent, decor); |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 808 | parent.getViewTreeObserver().addOnPreDrawListener(listener); |
| 809 | mGhostViewListeners.add(listener); |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | protected boolean moveSharedElementWithParent() { |
| 816 | return true; |
| 817 | } |
| 818 | |
| 819 | public static boolean isInTransitionGroup(ViewParent viewParent, ViewGroup decor) { |
| 820 | if (viewParent == decor || !(viewParent instanceof ViewGroup)) { |
| 821 | return false; |
| 822 | } |
| 823 | ViewGroup parent = (ViewGroup) viewParent; |
| 824 | if (parent.isTransitionGroup()) { |
| 825 | return true; |
| 826 | } else { |
| 827 | return isInTransitionGroup(parent.getParent(), decor); |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | protected void moveSharedElementsFromOverlay() { |
George Mount | b89d5cc | 2014-08-18 16:50:50 -0700 | [diff] [blame] | 832 | int numListeners = mGhostViewListeners.size(); |
| 833 | for (int i = 0; i < numListeners; i++) { |
| 834 | GhostViewListeners listener = mGhostViewListeners.get(i); |
| 835 | ViewGroup parent = (ViewGroup) listener.getView().getParent(); |
| 836 | parent.getViewTreeObserver().removeOnPreDrawListener(listener); |
| 837 | } |
| 838 | mGhostViewListeners.clear(); |
| 839 | |
| 840 | if (mWindow == null || !mWindow.getSharedElementsUseOverlay()) { |
| 841 | return; |
| 842 | } |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 843 | ViewGroup decor = getDecor(); |
| 844 | if (decor != null) { |
| 845 | ViewGroupOverlay overlay = decor.getOverlay(); |
| 846 | int count = mSharedElements.size(); |
| 847 | for (int i = 0; i < count; i++) { |
| 848 | View sharedElement = mSharedElements.get(i); |
| 849 | GhostView.removeGhost(sharedElement); |
| 850 | } |
| 851 | } |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | protected void setGhostVisibility(int visibility) { |
| 855 | int numSharedElements = mSharedElements.size(); |
| 856 | for (int i = 0; i < numSharedElements; i++) { |
| 857 | GhostView ghostView = GhostView.getGhost(mSharedElements.get(i)); |
| 858 | if (ghostView != null) { |
| 859 | ghostView.setVisibility(visibility); |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | protected void scheduleGhostVisibilityChange(final int visibility) { |
George Mount | 6e7fb60 | 2014-09-04 16:20:20 -0700 | [diff] [blame] | 865 | final View decorView = getDecor(); |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 866 | if (decorView != null) { |
| 867 | decorView.getViewTreeObserver() |
| 868 | .addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { |
| 869 | @Override |
| 870 | public boolean onPreDraw() { |
| 871 | decorView.getViewTreeObserver().removeOnPreDrawListener(this); |
| 872 | setGhostVisibility(visibility); |
| 873 | return true; |
| 874 | } |
| 875 | }); |
| 876 | } |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 877 | } |
| 878 | |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 879 | protected boolean isViewsTransitionComplete() { |
| 880 | return mViewsTransitionComplete; |
| 881 | } |
| 882 | |
| 883 | protected void viewsTransitionComplete() { |
| 884 | mViewsTransitionComplete = true; |
| 885 | startInputWhenTransitionsComplete(); |
| 886 | } |
| 887 | |
| 888 | protected void sharedElementTransitionComplete() { |
| 889 | mSharedElementTransitionComplete = true; |
| 890 | startInputWhenTransitionsComplete(); |
| 891 | } |
| 892 | private void startInputWhenTransitionsComplete() { |
| 893 | if (mViewsTransitionComplete && mSharedElementTransitionComplete) { |
| 894 | final View decor = getDecor(); |
| 895 | if (decor != null) { |
| 896 | final ViewRootImpl viewRoot = decor.getViewRootImpl(); |
George Mount | 5fddd4c | 2016-01-14 08:24:20 -0800 | [diff] [blame] | 897 | if (viewRoot != null) { |
| 898 | viewRoot.setPausedForTransition(false); |
| 899 | } |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 900 | } |
| 901 | onTransitionsComplete(); |
| 902 | } |
| 903 | } |
| 904 | |
George Mount | 80141d1 | 2015-07-14 10:03:06 -0700 | [diff] [blame] | 905 | protected void pauseInput() { |
| 906 | final View decor = getDecor(); |
| 907 | final ViewRootImpl viewRoot = decor == null ? null : decor.getViewRootImpl(); |
| 908 | if (viewRoot != null) { |
| 909 | viewRoot.setPausedForTransition(true); |
| 910 | } |
| 911 | } |
| 912 | |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 913 | protected void onTransitionsComplete() {} |
| 914 | |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 915 | protected class ContinueTransitionListener extends Transition.TransitionListenerAdapter { |
| 916 | @Override |
| 917 | public void onTransitionStart(Transition transition) { |
| 918 | mIsStartingTransition = false; |
| 919 | Runnable pending = mPendingTransition; |
| 920 | mPendingTransition = null; |
| 921 | if (pending != null) { |
| 922 | startTransition(pending); |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 927 | private static int scaleTypeToInt(ImageView.ScaleType scaleType) { |
| 928 | for (int i = 0; i < SCALE_TYPE_VALUES.length; i++) { |
| 929 | if (scaleType == SCALE_TYPE_VALUES[i]) { |
| 930 | return i; |
| 931 | } |
| 932 | } |
| 933 | return -1; |
| 934 | } |
| 935 | |
George Mount | 6297672 | 2016-02-04 16:45:53 -0800 | [diff] [blame] | 936 | protected void setTransitioningViewsVisiblity(int visiblity, boolean invalidate) { |
| 937 | final int numElements = mTransitioningViews == null ? 0 : mTransitioningViews.size(); |
| 938 | for (int i = 0; i < numElements; i++) { |
| 939 | final View view = mTransitioningViews.get(i); |
| 940 | view.setTransitionVisibility(visiblity); |
| 941 | if (invalidate) { |
| 942 | view.invalidate(); |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 947 | private static class FixedEpicenterCallback extends Transition.EpicenterCallback { |
| 948 | private Rect mEpicenter; |
| 949 | |
| 950 | public void setEpicenter(Rect epicenter) { mEpicenter = epicenter; } |
| 951 | |
| 952 | @Override |
George Mount | dc21d3b | 2014-06-05 09:42:48 -0700 | [diff] [blame] | 953 | public Rect onGetEpicenter(Transition transition) { |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 954 | return mEpicenter; |
| 955 | } |
| 956 | } |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 957 | |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 958 | private static class GhostViewListeners implements ViewTreeObserver.OnPreDrawListener { |
| 959 | private View mView; |
| 960 | private ViewGroup mDecor; |
George Mount | 6e7fb60 | 2014-09-04 16:20:20 -0700 | [diff] [blame] | 961 | private View mParent; |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 962 | private Matrix mMatrix = new Matrix(); |
| 963 | |
George Mount | 6e7fb60 | 2014-09-04 16:20:20 -0700 | [diff] [blame] | 964 | public GhostViewListeners(View view, View parent, ViewGroup decor) { |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 965 | mView = view; |
George Mount | 6e7fb60 | 2014-09-04 16:20:20 -0700 | [diff] [blame] | 966 | mParent = parent; |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 967 | mDecor = decor; |
| 968 | } |
| 969 | |
| 970 | public View getView() { |
| 971 | return mView; |
| 972 | } |
| 973 | |
| 974 | @Override |
| 975 | public boolean onPreDraw() { |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 976 | GhostView ghostView = GhostView.getGhost(mView); |
| 977 | if (ghostView == null) { |
George Mount | 6e7fb60 | 2014-09-04 16:20:20 -0700 | [diff] [blame] | 978 | mParent.getViewTreeObserver().removeOnPreDrawListener(this); |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 979 | } else { |
| 980 | GhostView.calculateMatrix(mView, mDecor, mMatrix); |
| 981 | ghostView.setMatrix(mMatrix); |
| 982 | } |
| 983 | return true; |
| 984 | } |
| 985 | } |
| 986 | |
Dake Gu | c18f4cc | 2014-07-11 17:48:37 -0700 | [diff] [blame] | 987 | static class SharedElementOriginalState { |
| 988 | int mLeft; |
| 989 | int mTop; |
| 990 | int mRight; |
| 991 | int mBottom; |
| 992 | int mMeasuredWidth; |
| 993 | int mMeasuredHeight; |
| 994 | ImageView.ScaleType mScaleType; |
| 995 | Matrix mMatrix; |
George Mount | 26c82b6 | 2014-08-08 15:43:59 -0700 | [diff] [blame] | 996 | float mTranslationZ; |
| 997 | float mElevation; |
Dake Gu | c18f4cc | 2014-07-11 17:48:37 -0700 | [diff] [blame] | 998 | } |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 999 | } |