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; |
| 20 | import android.animation.AnimatorListenerAdapter; |
| 21 | import android.animation.ObjectAnimator; |
| 22 | import android.util.Log; |
| 23 | import android.view.View; |
| 24 | import android.view.ViewGroup; |
| 25 | |
| 26 | /** |
| 27 | * This transition tracks changes to the visibility of target views in the |
| 28 | * start and end scenes and fades views in or out when they become visible |
| 29 | * or non-visible. Visibility is determined by both the |
| 30 | * {@link View#setVisibility(int)} state of the view as well as whether it |
| 31 | * is parented in the current view hierarchy. |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 32 | * |
| 33 | * <p>A Fade transition can be described in a resource file by using the |
| 34 | * tag <code>fade</code>, along with the standard |
| 35 | * attributes of {@link android.R.styleable#Fade} and |
| 36 | * {@link android.R.styleable#Transition}.</p> |
| 37 | |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 38 | */ |
| 39 | public class Fade extends Visibility { |
| 40 | |
Chet Haase | c43524f | 2013-07-16 14:40:11 -0700 | [diff] [blame] | 41 | private static boolean DBG = Transition.DBG && false; |
| 42 | |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 43 | private static final String LOG_TAG = "Fade"; |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 44 | private static final String PROPNAME_ALPHA = "android:fade:alpha"; |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 45 | private static final String PROPNAME_SCREEN_X = "android:fade:screenX"; |
| 46 | private static final String PROPNAME_SCREEN_Y = "android:fade:screenY"; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Fading mode used in {@link #Fade(int)} to make the transition |
| 50 | * operate on targets that are appearing. Maybe be combined with |
| 51 | * {@link #OUT} to fade both in and out. |
| 52 | */ |
| 53 | public static final int IN = 0x1; |
| 54 | /** |
| 55 | * Fading mode used in {@link #Fade(int)} to make the transition |
| 56 | * operate on targets that are disappearing. Maybe be combined with |
| 57 | * {@link #IN} to fade both in and out. |
| 58 | */ |
| 59 | public static final int OUT = 0x2; |
| 60 | |
| 61 | private int mFadingMode; |
| 62 | |
| 63 | /** |
| 64 | * Constructs a Fade transition that will fade targets in and out. |
| 65 | */ |
| 66 | public Fade() { |
| 67 | this(IN | OUT); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Constructs a Fade transition that will fade targets in |
| 72 | * and/or out, according to the value of fadingMode. |
| 73 | * |
| 74 | * @param fadingMode The behavior of this transition, a combination of |
| 75 | * {@link #IN} and {@link #OUT}. |
| 76 | */ |
| 77 | public Fade(int fadingMode) { |
| 78 | mFadingMode = fadingMode; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Utility method to handle creating and running the Animator. |
| 83 | */ |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 84 | private Animator createAnimation(View view, float startAlpha, float endAlpha, |
| 85 | AnimatorListenerAdapter listener) { |
| 86 | if (startAlpha == endAlpha) { |
| 87 | // run listener if we're noop'ing the animation, to get the end-state results now |
| 88 | if (listener != null) { |
| 89 | listener.onAnimationEnd(null); |
| 90 | } |
| 91 | return null; |
| 92 | } |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 93 | final ObjectAnimator anim = ObjectAnimator.ofFloat(view, "alpha", startAlpha, endAlpha); |
| 94 | if (listener != null) { |
| 95 | anim.addListener(listener); |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 96 | anim.addPauseListener(listener); |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 97 | } |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 98 | return anim; |
| 99 | } |
| 100 | |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 101 | private void captureValues(TransitionValues transitionValues) { |
| 102 | float alpha = transitionValues.view.getAlpha(); |
| 103 | transitionValues.values.put(PROPNAME_ALPHA, alpha); |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 104 | int[] loc = new int[2]; |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 105 | transitionValues.view.getLocationOnScreen(loc); |
| 106 | transitionValues.values.put(PROPNAME_SCREEN_X, loc[0]); |
| 107 | transitionValues.values.put(PROPNAME_SCREEN_Y, loc[1]); |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 111 | public void captureStartValues(TransitionValues transitionValues) { |
| 112 | super.captureStartValues(transitionValues); |
| 113 | captureValues(transitionValues); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | @Override |
| 118 | public void captureEndValues(TransitionValues transitionValues) { |
| 119 | super.captureEndValues(transitionValues); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues, |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 124 | TransitionValues endValues) { |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 125 | Animator animator = super.createAnimator(sceneRoot, startValues, endValues); |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 126 | if (animator == null && startValues != null && endValues != null) { |
| 127 | boolean endVisible = isVisible(endValues); |
| 128 | final View endView = endValues.view; |
| 129 | float endAlpha = endView.getAlpha(); |
| 130 | float startAlpha = (Float) startValues.values.get(PROPNAME_ALPHA); |
| 131 | if ((endVisible && startAlpha < endAlpha && (mFadingMode & Fade.IN) != 0) || |
| 132 | (!endVisible && startAlpha > endAlpha && (mFadingMode & Fade.OUT) != 0)) { |
| 133 | animator = createAnimation(endView, startAlpha, endAlpha, null); |
| 134 | } |
| 135 | } |
| 136 | return animator; |
| 137 | } |
| 138 | |
| 139 | @Override |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 140 | public Animator onAppear(ViewGroup sceneRoot, |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 141 | TransitionValues startValues, int startVisibility, |
| 142 | TransitionValues endValues, int endVisibility) { |
Chet Haase | dc57d9d | 2013-07-10 11:27:54 -0700 | [diff] [blame] | 143 | if ((mFadingMode & IN) != IN || endValues == null) { |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 144 | return null; |
| 145 | } |
Chet Haase | dc57d9d | 2013-07-10 11:27:54 -0700 | [diff] [blame] | 146 | final View endView = endValues.view; |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 147 | if (DBG) { |
| 148 | View startView = (startValues != null) ? startValues.view : null; |
| 149 | Log.d(LOG_TAG, "Fade.onDisappear: startView, startVis, endView, endVis = " + |
| 150 | startView + ", " + startVisibility + ", " + endView + ", " + endVisibility); |
| 151 | } |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 152 | // if alpha < 1, just fade it in from the current value |
| 153 | if (endView.getAlpha() == 1.0f) { |
| 154 | endView.setAlpha(0); |
| 155 | } |
| 156 | return createAnimation(endView, endView.getAlpha(), 1, null); |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 160 | public Animator onDisappear(ViewGroup sceneRoot, |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 161 | TransitionValues startValues, int startVisibility, |
| 162 | TransitionValues endValues, int endVisibility) { |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 163 | if ((mFadingMode & OUT) != OUT) { |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 164 | return null; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 165 | } |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 166 | View view; |
| 167 | View startView = (startValues != null) ? startValues.view : null; |
| 168 | View endView = (endValues != null) ? endValues.view : null; |
Chet Haase | c43524f | 2013-07-16 14:40:11 -0700 | [diff] [blame] | 169 | if (DBG) { |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 170 | Log.d(LOG_TAG, "Fade.onDisappear: startView, startVis, endView, endVis = " + |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 171 | startView + ", " + startVisibility + ", " + endView + ", " + endVisibility); |
| 172 | } |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 173 | View overlayView = null; |
| 174 | View viewToKeep = null; |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 175 | if (endView == null || endView.getParent() == null) { |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 176 | // view was removed: add the start view to the Overlay |
| 177 | view = startView; |
| 178 | overlayView = view; |
| 179 | } else { |
| 180 | // visibility change |
| 181 | if (endVisibility == View.INVISIBLE) { |
| 182 | view = endView; |
| 183 | viewToKeep = view; |
| 184 | } else { |
| 185 | // Becoming GONE |
| 186 | if (startView == endView) { |
| 187 | view = endView; |
| 188 | viewToKeep = view; |
| 189 | } else { |
| 190 | view = startView; |
| 191 | overlayView = view; |
| 192 | } |
| 193 | } |
| 194 | } |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 195 | final int finalVisibility = endVisibility; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 196 | // TODO: add automatic facility to Visibility superclass for keeping views around |
| 197 | if (overlayView != null) { |
| 198 | // TODO: Need to do this for general case of adding to overlay |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 199 | int screenX = (Integer) startValues.values.get(PROPNAME_SCREEN_X); |
| 200 | int screenY = (Integer) startValues.values.get(PROPNAME_SCREEN_Y); |
| 201 | int[] loc = new int[2]; |
| 202 | sceneRoot.getLocationOnScreen(loc); |
| 203 | overlayView.offsetLeftAndRight((screenX - loc[0]) - overlayView.getLeft()); |
| 204 | overlayView.offsetTopAndBottom((screenY - loc[1]) - overlayView.getTop()); |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 205 | sceneRoot.getOverlay().add(overlayView); |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 206 | // TODO: add automatic facility to Visibility superclass for keeping views around |
| 207 | final float startAlpha = view.getAlpha(); |
| 208 | float endAlpha = 0; |
| 209 | final View finalView = view; |
| 210 | final View finalOverlayView = overlayView; |
| 211 | final View finalViewToKeep = viewToKeep; |
| 212 | final ViewGroup finalSceneRoot = sceneRoot; |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 213 | final AnimatorListenerAdapter endListener = new AnimatorListenerAdapter() { |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 214 | @Override |
| 215 | public void onAnimationEnd(Animator animation) { |
| 216 | finalView.setAlpha(startAlpha); |
| 217 | // TODO: restore view offset from overlay repositioning |
| 218 | if (finalViewToKeep != null) { |
| 219 | finalViewToKeep.setVisibility(finalVisibility); |
| 220 | } |
| 221 | if (finalOverlayView != null) { |
| 222 | finalSceneRoot.getOverlay().remove(finalOverlayView); |
| 223 | } |
| 224 | } |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 225 | |
| 226 | @Override |
| 227 | public void onAnimationPause(Animator animation) { |
| 228 | if (finalOverlayView != null) { |
| 229 | finalSceneRoot.getOverlay().remove(finalOverlayView); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public void onAnimationResume(Animator animation) { |
| 235 | if (finalOverlayView != null) { |
| 236 | finalSceneRoot.getOverlay().add(finalOverlayView); |
| 237 | } |
| 238 | } |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 239 | }; |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 240 | return createAnimation(view, startAlpha, endAlpha, endListener); |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 241 | } |
| 242 | if (viewToKeep != null) { |
| 243 | // TODO: find a different way to do this, like just changing the view to be |
| 244 | // VISIBLE for the duration of the transition |
| 245 | viewToKeep.setVisibility((View.VISIBLE)); |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 246 | // TODO: add automatic facility to Visibility superclass for keeping views around |
| 247 | final float startAlpha = view.getAlpha(); |
| 248 | float endAlpha = 0; |
| 249 | final View finalView = view; |
| 250 | final View finalOverlayView = overlayView; |
| 251 | final View finalViewToKeep = viewToKeep; |
| 252 | final ViewGroup finalSceneRoot = sceneRoot; |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 253 | final AnimatorListenerAdapter endListener = new AnimatorListenerAdapter() { |
| 254 | boolean mCanceled = false; |
| 255 | float mPausedAlpha = -1; |
| 256 | |
| 257 | @Override |
| 258 | public void onAnimationPause(Animator animation) { |
| 259 | if (finalViewToKeep != null && !mCanceled) { |
| 260 | finalViewToKeep.setVisibility(finalVisibility); |
| 261 | } |
| 262 | mPausedAlpha = finalView.getAlpha(); |
| 263 | finalView.setAlpha(startAlpha); |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public void onAnimationResume(Animator animation) { |
| 268 | if (finalViewToKeep != null && !mCanceled) { |
| 269 | finalViewToKeep.setVisibility(View.VISIBLE); |
| 270 | } |
| 271 | finalView.setAlpha(mPausedAlpha); |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public void onAnimationCancel(Animator animation) { |
| 276 | mCanceled = true; |
| 277 | if (mPausedAlpha >= 0) { |
| 278 | finalView.setAlpha(mPausedAlpha); |
| 279 | } |
| 280 | } |
| 281 | |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 282 | @Override |
| 283 | public void onAnimationEnd(Animator animation) { |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 284 | if (!mCanceled) { |
| 285 | finalView.setAlpha(startAlpha); |
| 286 | } |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 287 | // TODO: restore view offset from overlay repositioning |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 288 | if (finalViewToKeep != null && !mCanceled) { |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 289 | finalViewToKeep.setVisibility(finalVisibility); |
| 290 | } |
| 291 | if (finalOverlayView != null) { |
| 292 | finalSceneRoot.getOverlay().remove(finalOverlayView); |
| 293 | } |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 294 | } |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 295 | }; |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 296 | return createAnimation(view, startAlpha, endAlpha, endListener); |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 297 | } |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 298 | return null; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | } |