TransitionDrawablepublic class TransitionDrawable extends LayerDrawable implements Drawable.CallbackAn extension of LayerDrawables that is intended to cross-fade between
the first and second layer. To start the transition, call {@link #startTransition(int)}. To
display just the first layer, call {@link #resetTransition()}.
It can be defined in an XML file with the <transition> element.
Each Drawable in the transition is defined in a nested <item> .
|
Fields Summary |
---|
private static final int | TRANSITION_STARTINGA transition is about to start. | private static final int | TRANSITION_RUNNINGThe transition has started and the animation is in progress | private static final int | TRANSITION_NONENo transition will be applied | private int | mTransitionStateThe current state of the transition. One of {@link #TRANSITION_STARTING},
{@link #TRANSITION_RUNNING} and {@link #TRANSITION_NONE} | private boolean | mReverse | private long | mStartTimeMillis | private int | mFrom | private int | mTo | private int | mDuration | private int | mOriginalDuration | private int | mAlpha | private boolean | mCrossFade |
Constructors Summary |
---|
public TransitionDrawable(Drawable[] layers)Create a new transition drawable with the specified list of layers. At least
2 layers are required for this drawable to work properly.
this(new TransitionState(null, null), layers);
| TransitionDrawable()Create a new transition drawable with no layer. To work correctly, at least 2
layers must be added to this drawable.
this(new TransitionState(null, null));
| private TransitionDrawable(TransitionState state)
super(state);
| private TransitionDrawable(TransitionState state, Drawable[] layers)
super(layers, state);
|
Methods Summary |
---|
LayerState | createConstantState(LayerState state)
return new TransitionState((TransitionState) state, this);
| public void | draw(android.graphics.Canvas canvas)
boolean done = true;
switch (mTransitionState) {
case TRANSITION_STARTING:
mStartTimeMillis = SystemClock.uptimeMillis();
done = false;
mTransitionState = TRANSITION_RUNNING;
break;
case TRANSITION_RUNNING:
if (mStartTimeMillis >= 0) {
float normalized = (float)
(SystemClock.uptimeMillis() - mStartTimeMillis) / mDuration;
done = normalized >= 1.0f;
normalized = Math.min(normalized, 1.0f);
mAlpha = (int) (mFrom + (mTo - mFrom) * normalized);
}
break;
}
final int alpha = mAlpha;
final boolean crossFade = mCrossFade;
final ChildDrawable[] array = mLayerState.mChildren;
Drawable d;
d = array[0].mDrawable;
if (crossFade) {
d.setAlpha(255 - alpha);
}
d.draw(canvas);
if (crossFade) {
d.setAlpha(0xFF);
}
if (alpha > 0) {
d = array[1].mDrawable;
d.setAlpha(alpha);
d.draw(canvas);
d.setAlpha(0xFF);
}
if (!done) {
invalidateSelf();
}
| public boolean | isCrossFadeEnabled()Indicates whether the cross fade is enabled for this transition.
return mCrossFade;
| public void | resetTransition()Show only the first layer.
mAlpha = 0;
mTransitionState = TRANSITION_NONE;
invalidateSelf();
| public void | reverseTransition(int duration)Reverses the transition, picking up where the transition currently is.
If the transition is not currently running, this will start the transition
with the specified duration. If the transition is already running, the last
known duration will be used.
final long time = SystemClock.uptimeMillis();
// Animation is over
if (time - mStartTimeMillis > mDuration) {
if (mAlpha == 0) {
mFrom = 0;
mTo = 255;
mAlpha = 0;
mReverse = false;
} else {
mFrom = 255;
mTo = 0;
mAlpha = 255;
mReverse = true;
}
mDuration = mOriginalDuration = duration;
mTransitionState = TRANSITION_STARTING;
invalidateSelf();
return;
}
mReverse = !mReverse;
mFrom = mAlpha;
mTo = mReverse ? 0 : 255;
mDuration = (int) (mReverse ? time - mStartTimeMillis :
mOriginalDuration - (time - mStartTimeMillis));
mTransitionState = TRANSITION_STARTING;
| public void | setCrossFadeEnabled(boolean enabled)Enables or disables the cross fade of the drawables. When cross fade
is disabled, the first drawable is always drawn opaque. With cross
fade enabled, the first drawable is drawn with the opposite alpha of
the second drawable.
mCrossFade = enabled;
| public void | startTransition(int durationMillis)Begin the second layer on top of the first layer.
mFrom = 0;
mTo = 255;
mAlpha = 0;
mDuration = mOriginalDuration = durationMillis;
mReverse = false;
mTransitionState = TRANSITION_STARTING;
invalidateSelf();
|
|