2015-05-18 10:26:32 -07:00
|
|
|
package org.thoughtcrime.securesms.components;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.support.annotation.NonNull;
|
2015-05-20 14:18:39 -07:00
|
|
|
import android.support.v4.view.animation.FastOutSlowInInterpolator;
|
2015-05-18 10:26:32 -07:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.view.animation.Animation;
|
|
|
|
import android.view.animation.TranslateAnimation;
|
|
|
|
import android.widget.FrameLayout;
|
|
|
|
|
|
|
|
public class AnimatingToggle extends FrameLayout {
|
|
|
|
|
|
|
|
private static final int SPEED_MILLIS = 200;
|
|
|
|
|
2015-05-22 13:30:34 -07:00
|
|
|
private View current;
|
|
|
|
|
2015-05-18 10:26:32 -07:00
|
|
|
public AnimatingToggle(Context context) {
|
|
|
|
super(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
public AnimatingToggle(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
public AnimatingToggle(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
|
|
super(context, attrs, defStyleAttr);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void addView(@NonNull View child, int index, ViewGroup.LayoutParams params) {
|
|
|
|
super.addView(child, index, params);
|
|
|
|
|
2015-05-22 13:30:34 -07:00
|
|
|
if (getChildCount() == 1) {
|
|
|
|
current = child;
|
|
|
|
child.setVisibility(View.VISIBLE);
|
|
|
|
} else {
|
|
|
|
child.setVisibility(View.GONE);
|
|
|
|
}
|
2015-05-18 10:26:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void display(View view) {
|
2015-05-22 13:30:34 -07:00
|
|
|
if (view == current) return;
|
2015-05-18 10:26:32 -07:00
|
|
|
|
2015-05-22 13:30:34 -07:00
|
|
|
int oldViewIndex = getViewIndex(current);
|
2015-05-18 10:26:32 -07:00
|
|
|
int newViewIndex = getViewIndex(view);
|
|
|
|
|
|
|
|
int sign;
|
|
|
|
|
|
|
|
if (oldViewIndex < newViewIndex) sign = -1;
|
|
|
|
else sign = 1;
|
|
|
|
|
|
|
|
TranslateAnimation oldViewAnimation = createTranslation(0.0f, sign * 1.0f);
|
|
|
|
TranslateAnimation newViewAnimation = createTranslation(sign * -1.0f, 0.0f);
|
|
|
|
|
2015-05-22 13:30:34 -07:00
|
|
|
animateOut(current, oldViewAnimation);
|
|
|
|
animateIn(view, newViewAnimation);
|
2015-05-18 10:26:32 -07:00
|
|
|
|
2015-05-22 13:30:34 -07:00
|
|
|
current = view;
|
|
|
|
}
|
2015-05-18 10:26:32 -07:00
|
|
|
|
2015-05-22 13:30:34 -07:00
|
|
|
private void animateOut(final View view, TranslateAnimation animation) {
|
2015-05-18 10:26:32 -07:00
|
|
|
animation.setAnimationListener(new Animation.AnimationListener() {
|
|
|
|
@Override
|
|
|
|
public void onAnimationStart(Animation animation) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAnimationEnd(Animation animation) {
|
|
|
|
view.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAnimationRepeat(Animation animation) {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
view.startAnimation(animation);
|
|
|
|
}
|
|
|
|
|
2015-05-22 13:30:34 -07:00
|
|
|
private void animateIn(View view, TranslateAnimation animation) {
|
2015-05-20 14:18:39 -07:00
|
|
|
animation.setInterpolator(new FastOutSlowInInterpolator());
|
2015-05-18 10:26:32 -07:00
|
|
|
view.setVisibility(View.VISIBLE);
|
|
|
|
view.startAnimation(animation);
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getViewIndex(View view) {
|
|
|
|
for (int i=0;i<getChildCount();i++) {
|
|
|
|
if (getChildAt(i) == view) return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new IllegalArgumentException("Not a parent of this view.");
|
|
|
|
}
|
|
|
|
|
|
|
|
private TranslateAnimation createTranslation(float startY, float endY) {
|
|
|
|
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
|
|
|
|
Animation.RELATIVE_TO_SELF, 0.0f,
|
|
|
|
Animation.RELATIVE_TO_SELF, startY,
|
|
|
|
Animation.RELATIVE_TO_SELF, endY);
|
|
|
|
|
|
|
|
translateAnimation.setDuration(SPEED_MILLIS);
|
2015-05-22 13:30:34 -07:00
|
|
|
translateAnimation.setInterpolator(new FastOutSlowInInterpolator());
|
2015-05-18 10:26:32 -07:00
|
|
|
|
|
|
|
return translateAnimation;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|