Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 16 | |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 17 | package android.view.transition; |
| 18 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
| 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. |
| 32 | */ |
| 33 | public class Fade extends Visibility { |
| 34 | |
Chet Haase | c43524f | 2013-07-16 14:40:11 -0700 | [diff] [blame] | 35 | private static boolean DBG = Transition.DBG && false; |
| 36 | |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 37 | private static final String LOG_TAG = "Fade"; |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 38 | private static final String PROPNAME_SCREEN_X = "android:fade:screenX"; |
| 39 | private static final String PROPNAME_SCREEN_Y = "android:fade:screenY"; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Fading mode used in {@link #Fade(int)} to make the transition |
| 43 | * operate on targets that are appearing. Maybe be combined with |
| 44 | * {@link #OUT} to fade both in and out. |
| 45 | */ |
| 46 | public static final int IN = 0x1; |
| 47 | /** |
| 48 | * Fading mode used in {@link #Fade(int)} to make the transition |
| 49 | * operate on targets that are disappearing. Maybe be combined with |
| 50 | * {@link #IN} to fade both in and out. |
| 51 | */ |
| 52 | public static final int OUT = 0x2; |
| 53 | |
| 54 | private int mFadingMode; |
| 55 | |
| 56 | /** |
| 57 | * Constructs a Fade transition that will fade targets in and out. |
| 58 | */ |
| 59 | public Fade() { |
| 60 | this(IN | OUT); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Constructs a Fade transition that will fade targets in |
| 65 | * and/or out, according to the value of fadingMode. |
| 66 | * |
| 67 | * @param fadingMode The behavior of this transition, a combination of |
| 68 | * {@link #IN} and {@link #OUT}. |
| 69 | */ |
| 70 | public Fade(int fadingMode) { |
| 71 | mFadingMode = fadingMode; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Utility method to handle creating and running the Animator. |
| 76 | */ |
| 77 | private Animator runAnimation(View view, float startAlpha, float endAlpha, |
| 78 | Animator.AnimatorListener listener) { |
| 79 | final ObjectAnimator anim = ObjectAnimator.ofFloat(view, "alpha", startAlpha, endAlpha); |
| 80 | if (listener != null) { |
| 81 | anim.addListener(listener); |
| 82 | } |
| 83 | // TODO: Maybe extract a method into Transition to run an animation that handles the |
| 84 | // duration/startDelay stuff for all subclasses. |
| 85 | return anim; |
| 86 | } |
| 87 | |
| 88 | @Override |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 89 | protected void captureValues(TransitionValues values, boolean start) { |
| 90 | super.captureValues(values, start); |
| 91 | int[] loc = new int[2]; |
| 92 | values.view.getLocationOnScreen(loc); |
| 93 | values.values.put(PROPNAME_SCREEN_X, loc[0]); |
| 94 | values.values.put(PROPNAME_SCREEN_Y, loc[1]); |
| 95 | } |
| 96 | |
| 97 | @Override |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 98 | protected Animator appear(ViewGroup sceneRoot, |
| 99 | TransitionValues startValues, int startVisibility, |
| 100 | TransitionValues endValues, int endVisibility) { |
Chet Haase | dc57d9d | 2013-07-10 11:27:54 -0700 | [diff] [blame] | 101 | if ((mFadingMode & IN) != IN || endValues == null) { |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 102 | return null; |
| 103 | } |
Chet Haase | dc57d9d | 2013-07-10 11:27:54 -0700 | [diff] [blame] | 104 | final View endView = endValues.view; |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 105 | endView.setAlpha(0); |
Chet Haase | dc57d9d | 2013-07-10 11:27:54 -0700 | [diff] [blame] | 106 | final Animator.AnimatorListener endListener = new AnimatorListenerAdapter() { |
| 107 | @Override |
| 108 | public void onAnimationEnd(Animator animation) { |
| 109 | // Always end animation with full alpha, in case it's canceled mid-stream |
| 110 | endView.setAlpha(1); |
| 111 | } |
| 112 | }; |
| 113 | return runAnimation(endView, 0, 1, endListener); |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 117 | protected Animator disappear(ViewGroup sceneRoot, |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 118 | TransitionValues startValues, int startVisibility, |
| 119 | TransitionValues endValues, int endVisibility) { |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 120 | if ((mFadingMode & OUT) != OUT) { |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 121 | return null; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 122 | } |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 123 | View view; |
| 124 | View startView = (startValues != null) ? startValues.view : null; |
| 125 | View endView = (endValues != null) ? endValues.view : null; |
Chet Haase | c43524f | 2013-07-16 14:40:11 -0700 | [diff] [blame] | 126 | if (DBG) { |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 127 | Log.d(LOG_TAG, "Fade.predisappear: startView, startVis, endView, endVis = " + |
| 128 | startView + ", " + startVisibility + ", " + endView + ", " + endVisibility); |
| 129 | } |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 130 | View overlayView = null; |
| 131 | View viewToKeep = null; |
| 132 | if (endView == null) { |
| 133 | // view was removed: add the start view to the Overlay |
| 134 | view = startView; |
| 135 | overlayView = view; |
| 136 | } else { |
| 137 | // visibility change |
| 138 | if (endVisibility == View.INVISIBLE) { |
| 139 | view = endView; |
| 140 | viewToKeep = view; |
| 141 | } else { |
| 142 | // Becoming GONE |
| 143 | if (startView == endView) { |
| 144 | view = endView; |
| 145 | viewToKeep = view; |
| 146 | } else { |
| 147 | view = startView; |
| 148 | overlayView = view; |
| 149 | } |
| 150 | } |
| 151 | } |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 152 | final int finalVisibility = endVisibility; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 153 | // TODO: add automatic facility to Visibility superclass for keeping views around |
| 154 | if (overlayView != null) { |
| 155 | // TODO: Need to do this for general case of adding to overlay |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 156 | int screenX = (Integer) startValues.values.get(PROPNAME_SCREEN_X); |
| 157 | int screenY = (Integer) startValues.values.get(PROPNAME_SCREEN_Y); |
| 158 | int[] loc = new int[2]; |
| 159 | sceneRoot.getLocationOnScreen(loc); |
| 160 | overlayView.offsetLeftAndRight((screenX - loc[0]) - overlayView.getLeft()); |
| 161 | overlayView.offsetTopAndBottom((screenY - loc[1]) - overlayView.getTop()); |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 162 | sceneRoot.getOverlay().add(overlayView); |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 163 | // TODO: add automatic facility to Visibility superclass for keeping views around |
| 164 | final float startAlpha = view.getAlpha(); |
| 165 | float endAlpha = 0; |
| 166 | final View finalView = view; |
| 167 | final View finalOverlayView = overlayView; |
| 168 | final View finalViewToKeep = viewToKeep; |
| 169 | final ViewGroup finalSceneRoot = sceneRoot; |
| 170 | final Animator.AnimatorListener endListener = new AnimatorListenerAdapter() { |
| 171 | @Override |
| 172 | public void onAnimationEnd(Animator animation) { |
| 173 | finalView.setAlpha(startAlpha); |
| 174 | // TODO: restore view offset from overlay repositioning |
| 175 | if (finalViewToKeep != null) { |
| 176 | finalViewToKeep.setVisibility(finalVisibility); |
| 177 | } |
| 178 | if (finalOverlayView != null) { |
| 179 | finalSceneRoot.getOverlay().remove(finalOverlayView); |
| 180 | } |
| 181 | } |
| 182 | }; |
| 183 | return runAnimation(view, startAlpha, endAlpha, endListener); |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 184 | } |
| 185 | if (viewToKeep != null) { |
| 186 | // TODO: find a different way to do this, like just changing the view to be |
| 187 | // VISIBLE for the duration of the transition |
| 188 | viewToKeep.setVisibility((View.VISIBLE)); |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 189 | // TODO: add automatic facility to Visibility superclass for keeping views around |
| 190 | final float startAlpha = view.getAlpha(); |
| 191 | float endAlpha = 0; |
| 192 | final View finalView = view; |
| 193 | final View finalOverlayView = overlayView; |
| 194 | final View finalViewToKeep = viewToKeep; |
| 195 | final ViewGroup finalSceneRoot = sceneRoot; |
| 196 | final Animator.AnimatorListener endListener = new AnimatorListenerAdapter() { |
| 197 | @Override |
| 198 | public void onAnimationEnd(Animator animation) { |
| 199 | finalView.setAlpha(startAlpha); |
| 200 | // TODO: restore view offset from overlay repositioning |
| 201 | if (finalViewToKeep != null) { |
| 202 | finalViewToKeep.setVisibility(finalVisibility); |
| 203 | } |
| 204 | if (finalOverlayView != null) { |
| 205 | finalSceneRoot.getOverlay().remove(finalOverlayView); |
| 206 | } |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 207 | } |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 208 | }; |
| 209 | return runAnimation(view, startAlpha, endAlpha, endListener); |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 210 | } |
Chet Haase | 2ea7f8b | 2013-06-21 15:00:05 -0700 | [diff] [blame] | 211 | return null; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | } |