2013-02-01 20:21:20 -05:00
|
|
|
package com.fsck.k9.view;
|
|
|
|
|
2013-02-06 04:53:42 -05:00
|
|
|
import com.fsck.k9.K9;
|
|
|
|
|
2013-02-01 20:21:20 -05:00
|
|
|
import android.content.Context;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.animation.Animation;
|
2013-02-05 18:56:33 -05:00
|
|
|
import android.view.animation.Animation.AnimationListener;
|
2013-02-01 20:21:20 -05:00
|
|
|
import android.widget.ViewAnimator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A {@link ViewAnimator} that animates between two child views using different animations
|
|
|
|
* depending on which view is displayed.
|
|
|
|
*/
|
2013-02-05 18:56:33 -05:00
|
|
|
public class ViewSwitcher extends ViewAnimator implements AnimationListener {
|
2013-02-01 20:21:20 -05:00
|
|
|
private Animation mFirstInAnimation;
|
|
|
|
private Animation mFirstOutAnimation;
|
|
|
|
private Animation mSecondInAnimation;
|
|
|
|
private Animation mSecondOutAnimation;
|
2013-02-06 14:44:06 -05:00
|
|
|
private OnSwitchCompleteListener mListener;
|
2013-02-01 20:21:20 -05:00
|
|
|
|
|
|
|
|
|
|
|
public ViewSwitcher(Context context) {
|
|
|
|
super(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ViewSwitcher(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void showFirstView() {
|
|
|
|
if (getDisplayedChild() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-06 04:53:42 -05:00
|
|
|
setupAnimations(mFirstInAnimation, mFirstOutAnimation);
|
2013-02-01 20:21:20 -05:00
|
|
|
setDisplayedChild(0);
|
2013-02-06 14:44:06 -05:00
|
|
|
handleSwitchCompleteCallback();
|
2013-02-01 20:21:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void showSecondView() {
|
|
|
|
if (getDisplayedChild() == 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-06 04:53:42 -05:00
|
|
|
setupAnimations(mSecondInAnimation, mSecondOutAnimation);
|
2013-02-01 20:21:20 -05:00
|
|
|
setDisplayedChild(1);
|
2013-02-06 14:44:06 -05:00
|
|
|
handleSwitchCompleteCallback();
|
2013-02-01 20:21:20 -05:00
|
|
|
}
|
|
|
|
|
2013-02-06 04:53:42 -05:00
|
|
|
private void setupAnimations(Animation in, Animation out) {
|
|
|
|
if (K9.showAnimations()) {
|
|
|
|
setInAnimation(in);
|
|
|
|
setOutAnimation(out);
|
2013-02-06 14:44:06 -05:00
|
|
|
out.setAnimationListener(this);
|
2013-02-06 04:53:42 -05:00
|
|
|
} else {
|
|
|
|
setInAnimation(null);
|
|
|
|
setOutAnimation(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-06 14:44:06 -05:00
|
|
|
private void handleSwitchCompleteCallback() {
|
|
|
|
if (!K9.showAnimations()) {
|
|
|
|
onAnimationEnd(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-01 20:21:20 -05:00
|
|
|
public Animation getFirstInAnimation() {
|
|
|
|
return mFirstInAnimation;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setFirstInAnimation(Animation inAnimation) {
|
|
|
|
this.mFirstInAnimation = inAnimation;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Animation getmFirstOutAnimation() {
|
|
|
|
return mFirstOutAnimation;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setFirstOutAnimation(Animation outAnimation) {
|
|
|
|
mFirstOutAnimation = outAnimation;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Animation getSecondInAnimation() {
|
|
|
|
return mSecondInAnimation;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setSecondInAnimation(Animation inAnimation) {
|
|
|
|
mSecondInAnimation = inAnimation;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Animation getSecondOutAnimation() {
|
|
|
|
return mSecondOutAnimation;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setSecondOutAnimation(Animation outAnimation) {
|
|
|
|
mSecondOutAnimation = outAnimation;
|
|
|
|
}
|
2013-02-05 18:56:33 -05:00
|
|
|
|
2013-02-06 14:44:06 -05:00
|
|
|
public void setOnSwitchCompleteListener(OnSwitchCompleteListener listener) {
|
2013-02-05 18:56:33 -05:00
|
|
|
mListener = listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAnimationEnd(Animation animation) {
|
|
|
|
if (mListener != null) {
|
2013-02-06 14:44:06 -05:00
|
|
|
mListener.onSwitchComplete(getDisplayedChild());
|
2013-02-05 18:56:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAnimationRepeat(Animation animation) {
|
|
|
|
// unused
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAnimationStart(Animation animation) {
|
|
|
|
// unused
|
|
|
|
}
|
|
|
|
|
2013-02-06 14:44:06 -05:00
|
|
|
public interface OnSwitchCompleteListener {
|
2013-02-05 18:56:33 -05:00
|
|
|
/**
|
2013-02-06 14:44:06 -05:00
|
|
|
* This method will be called after the switch (including animation) has ended.
|
2013-02-05 18:56:33 -05:00
|
|
|
*
|
|
|
|
* @param displayedChild
|
|
|
|
* Contains the zero-based index of the child view that is now displayed.
|
|
|
|
*/
|
2013-02-06 14:44:06 -05:00
|
|
|
void onSwitchComplete(int displayedChild);
|
2013-02-05 18:56:33 -05:00
|
|
|
}
|
2013-02-01 20:21:20 -05:00
|
|
|
}
|