Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 16 | |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 17 | package android.transition; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 18 | |
George Mount | ecd857b | 2014-06-19 07:51:08 -0700 | [diff] [blame] | 19 | import com.android.internal.R; |
| 20 | |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 21 | import android.animation.Animator; |
| 22 | import android.animation.AnimatorListenerAdapter; |
| 23 | import android.animation.ObjectAnimator; |
George Mount | ecd857b | 2014-06-19 07:51:08 -0700 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.res.TypedArray; |
| 26 | import android.util.AttributeSet; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 27 | import android.util.Log; |
| 28 | import android.view.View; |
| 29 | import android.view.ViewGroup; |
| 30 | |
| 31 | /** |
| 32 | * This transition tracks changes to the visibility of target views in the |
| 33 | * start and end scenes and fades views in or out when they become visible |
| 34 | * or non-visible. Visibility is determined by both the |
| 35 | * {@link View#setVisibility(int)} state of the view as well as whether it |
| 36 | * is parented in the current view hierarchy. |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 37 | * |
Chet Haase | b7a7fc9 | 2013-09-20 16:33:08 -0700 | [diff] [blame] | 38 | * <p>The ability of this transition to fade out a particular view, and the |
| 39 | * way that that fading operation takes place, is based on |
| 40 | * the situation of the view in the view hierarchy. For example, if a view was |
| 41 | * simply removed from its parent, then the view will be added into a {@link |
| 42 | * android.view.ViewGroupOverlay} while fading. If a visible view is |
| 43 | * changed to be {@link View#GONE} or {@link View#INVISIBLE}, then the |
| 44 | * visibility will be changed to {@link View#VISIBLE} for the duration of |
| 45 | * the animation. However, if a view is in a hierarchy which is also altering |
| 46 | * its visibility, the situation can be more complicated. In general, if a |
| 47 | * view that is no longer in the hierarchy in the end scene still has a |
| 48 | * parent (so its parent hierarchy was removed, but it was not removed from |
| 49 | * its parent), then it will be left alone to avoid side-effects from |
| 50 | * improperly removing it from its parent. The only exception to this is if |
| 51 | * the previous {@link Scene} was |
| 52 | * {@link Scene#getSceneForLayout(android.view.ViewGroup, int, android.content.Context) |
| 53 | * created from a layout resource file}, then it is considered safe to un-parent |
| 54 | * the starting scene view in order to fade it out.</p> |
| 55 | * |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 56 | * <p>A Fade transition can be described in a resource file by using the |
| 57 | * tag <code>fade</code>, along with the standard |
| 58 | * attributes of {@link android.R.styleable#Fade} and |
| 59 | * {@link android.R.styleable#Transition}.</p> |
| 60 | |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 61 | */ |
| 62 | public class Fade extends Visibility { |
| 63 | |
Chet Haase | c43524f | 2013-07-16 14:40:11 -0700 | [diff] [blame] | 64 | private static boolean DBG = Transition.DBG && false; |
| 65 | |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 66 | private static final String LOG_TAG = "Fade"; |
| 67 | |
| 68 | /** |
| 69 | * Fading mode used in {@link #Fade(int)} to make the transition |
| 70 | * operate on targets that are appearing. Maybe be combined with |
George Mount | 18ab799 | 2014-06-25 17:04:51 -0700 | [diff] [blame] | 71 | * {@link #OUT} to fade both in and out. Equivalent to |
George Mount | ad88e1b | 2014-07-18 15:41:13 -0700 | [diff] [blame] | 72 | * {@link Visibility#MODE_IN}. |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 73 | */ |
George Mount | ad88e1b | 2014-07-18 15:41:13 -0700 | [diff] [blame] | 74 | public static final int IN = Visibility.MODE_IN; |
George Mount | 18ab799 | 2014-06-25 17:04:51 -0700 | [diff] [blame] | 75 | |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 76 | /** |
| 77 | * Fading mode used in {@link #Fade(int)} to make the transition |
| 78 | * operate on targets that are disappearing. Maybe be combined with |
George Mount | 18ab799 | 2014-06-25 17:04:51 -0700 | [diff] [blame] | 79 | * {@link #IN} to fade both in and out. Equivalent to |
George Mount | ad88e1b | 2014-07-18 15:41:13 -0700 | [diff] [blame] | 80 | * {@link Visibility#MODE_OUT}. |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 81 | */ |
George Mount | ad88e1b | 2014-07-18 15:41:13 -0700 | [diff] [blame] | 82 | public static final int OUT = Visibility.MODE_OUT; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Constructs a Fade transition that will fade targets in and out. |
| 86 | */ |
| 87 | public Fade() { |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Constructs a Fade transition that will fade targets in |
| 92 | * and/or out, according to the value of fadingMode. |
| 93 | * |
| 94 | * @param fadingMode The behavior of this transition, a combination of |
| 95 | * {@link #IN} and {@link #OUT}. |
| 96 | */ |
| 97 | public Fade(int fadingMode) { |
George Mount | 18ab799 | 2014-06-25 17:04:51 -0700 | [diff] [blame] | 98 | setMode(fadingMode); |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 99 | } |
| 100 | |
George Mount | ecd857b | 2014-06-19 07:51:08 -0700 | [diff] [blame] | 101 | public Fade(Context context, AttributeSet attrs) { |
| 102 | super(context, attrs); |
| 103 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Fade); |
| 104 | int fadingMode = a.getInt(R.styleable.Fade_fadingMode, getMode()); |
| 105 | setMode(fadingMode); |
| 106 | } |
| 107 | |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 108 | /** |
| 109 | * Utility method to handle creating and running the Animator. |
| 110 | */ |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 111 | private Animator createAnimation(View view, float startAlpha, float endAlpha) { |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 112 | if (startAlpha == endAlpha) { |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 113 | return null; |
| 114 | } |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 115 | view.setTransitionAlpha(startAlpha); |
| 116 | final ObjectAnimator anim = ObjectAnimator.ofFloat(view, "transitionAlpha", endAlpha); |
Chet Haase | 23c61f6 | 2013-09-14 11:28:46 -0700 | [diff] [blame] | 117 | if (DBG) { |
| 118 | Log.d(LOG_TAG, "Created animator " + anim); |
| 119 | } |
George Mount | 1b2fb2c | 2014-04-25 17:07:58 -0700 | [diff] [blame] | 120 | FadeAnimatorListener listener = new FadeAnimatorListener(view); |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 121 | anim.addListener(listener); |
| 122 | anim.addPauseListener(listener); |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 123 | return anim; |
| 124 | } |
| 125 | |
Chet Haase | 6ebe3de | 2013-06-17 16:50:50 -0700 | [diff] [blame] | 126 | @Override |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 127 | public Animator onAppear(ViewGroup sceneRoot, View view, |
| 128 | TransitionValues startValues, |
| 129 | TransitionValues endValues) { |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 130 | if (DBG) { |
| 131 | View startView = (startValues != null) ? startValues.view : null; |
Chet Haase | 23c61f6 | 2013-09-14 11:28:46 -0700 | [diff] [blame] | 132 | Log.d(LOG_TAG, "Fade.onAppear: startView, startVis, endView, endVis = " + |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 133 | startView + ", " + view); |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 134 | } |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 135 | return createAnimation(view, 0, 1); |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 139 | public Animator onDisappear(ViewGroup sceneRoot, final View view, TransitionValues startValues, |
| 140 | TransitionValues endValues) { |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 141 | return createAnimation(view, 1, 0); |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 142 | } |
| 143 | |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 144 | private static class FadeAnimatorListener extends AnimatorListenerAdapter { |
| 145 | private final View mView; |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 146 | private boolean mCanceled = false; |
George Mount | 1b2fb2c | 2014-04-25 17:07:58 -0700 | [diff] [blame] | 147 | private float mPausedAlpha = -1; |
George Mount | dc5bf6a | 2014-09-17 15:28:06 -0700 | [diff] [blame] | 148 | private boolean mLayerTypeChanged = false; |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 149 | |
George Mount | 1b2fb2c | 2014-04-25 17:07:58 -0700 | [diff] [blame] | 150 | public FadeAnimatorListener(View view) { |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 151 | mView = view; |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
George Mount | dc5bf6a | 2014-09-17 15:28:06 -0700 | [diff] [blame] | 155 | public void onAnimationStart(Animator animator) { |
| 156 | if (mView.hasOverlappingRendering() && mView.getLayerType() == View.LAYER_TYPE_NONE) { |
| 157 | mLayerTypeChanged = true; |
| 158 | mView.setLayerType(View.LAYER_TYPE_HARDWARE, null); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | @Override |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 163 | public void onAnimationCancel(Animator animator) { |
| 164 | mCanceled = true; |
| 165 | if (mPausedAlpha >= 0) { |
| 166 | mView.setTransitionAlpha(mPausedAlpha); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void onAnimationEnd(Animator animator) { |
| 172 | if (!mCanceled) { |
George Mount | 1b2fb2c | 2014-04-25 17:07:58 -0700 | [diff] [blame] | 173 | mView.setTransitionAlpha(1); |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 174 | } |
George Mount | dc5bf6a | 2014-09-17 15:28:06 -0700 | [diff] [blame] | 175 | if (mLayerTypeChanged) { |
| 176 | mView.setLayerType(View.LAYER_TYPE_NONE, null); |
| 177 | } |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public void onAnimationPause(Animator animator) { |
| 182 | mPausedAlpha = mView.getTransitionAlpha(); |
George Mount | 1b2fb2c | 2014-04-25 17:07:58 -0700 | [diff] [blame] | 183 | mView.setTransitionAlpha(1); |
George Mount | d6107a3 | 2014-03-10 16:51:16 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public void onAnimationResume(Animator animator) { |
| 188 | mView.setTransitionAlpha(mPausedAlpha); |
| 189 | } |
| 190 | } |
| 191 | } |