| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.transition; |
| 18 |
|
| 19 |
import android.animation.Animator; |
| 20 |
import android.animation.AnimatorListenerAdapter; |
| 21 |
import android.animation.ObjectAnimator; |
| 22 |
import android.content.Context; |
| 23 |
import android.content.res.TypedArray; |
| 24 |
import android.util.AttributeSet; |
| 25 |
import android.util.Log; |
| 26 |
import android.view.View; |
| 27 |
import android.view.ViewGroup; |
| 28 |
|
| 29 |
import com.android.internal.R; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public class Fade extends Visibility { |
| 62 |
static final String PROPNAME_TRANSITION_ALPHA = "android:fade:transitionAlpha"; |
| 63 |
|
| 64 |
private static boolean DBG = Transition.DBG && false; |
| 65 |
|
| 66 |
private static final String LOG_TAG = "Fade"; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public static final int IN = Visibility.MODE_IN; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public static final int OUT = Visibility.MODE_OUT; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public Fade() { |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public Fade(@VisibilityMode int fadingMode) { |
| 98 |
setMode(fadingMode); |
| 99 |
} |
| 100 |
|
| 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 |
a.recycle(); |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
public void captureStartValues(TransitionValues transitionValues) { |
| 111 |
super.captureStartValues(transitionValues); |
| 112 |
transitionValues.values.put(PROPNAME_TRANSITION_ALPHA, |
| 113 |
transitionValues.view.getTransitionAlpha()); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
private Animator createAnimation(final View view, float startAlpha, final float endAlpha) { |
| 120 |
if (startAlpha == endAlpha) { |
| 121 |
return null; |
| 122 |
} |
| 123 |
view.setTransitionAlpha(startAlpha); |
| 124 |
final ObjectAnimator anim = ObjectAnimator.ofFloat(view, "transitionAlpha", endAlpha); |
| 125 |
if (DBG) { |
| 126 |
Log.d(LOG_TAG, "Created animator " + anim); |
| 127 |
} |
| 128 |
final FadeAnimatorListener listener = new FadeAnimatorListener(view); |
| 129 |
anim.addListener(listener); |
| 130 |
addListener(new TransitionListenerAdapter() { |
| 131 |
@Override |
| 132 |
public void onTransitionEnd(Transition transition) { |
| 133 |
view.setTransitionAlpha(1); |
| 134 |
transition.removeListener(this); |
| 135 |
} |
| 136 |
}); |
| 137 |
return anim; |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public Animator onAppear(ViewGroup sceneRoot, View view, |
| 142 |
TransitionValues startValues, |
| 143 |
TransitionValues endValues) { |
| 144 |
if (DBG) { |
| 145 |
View startView = (startValues != null) ? startValues.view : null; |
| 146 |
Log.d(LOG_TAG, "Fade.onAppear: startView, startVis, endView, endVis = " + |
| 147 |
startView + ", " + view); |
| 148 |
} |
| 149 |
float startAlpha = getStartAlpha(startValues, 0); |
| 150 |
if (startAlpha == 1) { |
| 151 |
startAlpha = 0; |
| 152 |
} |
| 153 |
return createAnimation(view, startAlpha, 1); |
| 154 |
} |
| 155 |
|
| 156 |
@Override |
| 157 |
public Animator onDisappear(ViewGroup sceneRoot, final View view, TransitionValues startValues, |
| 158 |
TransitionValues endValues) { |
| 159 |
float startAlpha = getStartAlpha(startValues, 1); |
| 160 |
return createAnimation(view, startAlpha, 0); |
| 161 |
} |
| 162 |
|
| 163 |
private static float getStartAlpha(TransitionValues startValues, float fallbackValue) { |
| 164 |
float startAlpha = fallbackValue; |
| 165 |
if (startValues != null) { |
| 166 |
Float startAlphaFloat = (Float) startValues.values.get(PROPNAME_TRANSITION_ALPHA); |
| 167 |
if (startAlphaFloat != null) { |
| 168 |
startAlpha = startAlphaFloat; |
| 169 |
} |
| 170 |
} |
| 171 |
return startAlpha; |
| 172 |
} |
| 173 |
|
| 174 |
private static class FadeAnimatorListener extends AnimatorListenerAdapter { |
| 175 |
private final View mView; |
| 176 |
private boolean mLayerTypeChanged = false; |
| 177 |
|
| 178 |
public FadeAnimatorListener(View view) { |
| 179 |
mView = view; |
| 180 |
} |
| 181 |
|
| 182 |
@Override |
| 183 |
public void onAnimationStart(Animator animator) { |
| 184 |
if (mView.hasOverlappingRendering() && mView.getLayerType() == View.LAYER_TYPE_NONE) { |
| 185 |
mLayerTypeChanged = true; |
| 186 |
mView.setLayerType(View.LAYER_TYPE_HARDWARE, null); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
@Override |
| 191 |
public void onAnimationEnd(Animator animator) { |
| 192 |
mView.setTransitionAlpha(1); |
| 193 |
if (mLayerTypeChanged) { |
| 194 |
mView.setLayerType(View.LAYER_TYPE_NONE, null); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
} |