package com.aincc.lib.ui.anim;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.PointF;
import android.util.FloatMath;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import com.aincc.lib.ui.widget.button.AButton;
/**
*
* <h3><b>Animationz</b></h3></br>
*
* Animation 모음..
*
* @author aincc@barusoft.com
* @version 1.0.0
* @since 1.0.0
*/
public class Animationz
{
/**
* 기본 회전 애니메이션
*
* @since 1.0.0
* @param context
* @param fromDegree
* @param toDegree
* @param duration
* @param interpolator
* (ex: android.R.anim.anticipate_overshoot_interpolator)
* @param fillafter
* @return the animation
*/
public static Animation rotate(Context context, float fromDegree, float toDegree, int duration, int interpolator, boolean fillafter)
{
Animation rotate = new RotateAnimation(fromDegree, toDegree, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
if (null != context && 0 != interpolator)
{
rotate.setInterpolator(AnimationUtils.loadInterpolator(context, interpolator));
}
rotate.setFillAfter(fillafter);
rotate.setDuration(duration);
return rotate;
}
/**
* 기본 이동 애니메이션
*
* @since 1.0.0
* @param context
* @param fromDelta
* @param toDelta
* @param duration
* @param interpolator
* @param fillafter
* @return the animation
*/
public static Animation translate(Context context, PointF fromDelta, PointF toDelta, int duration, int interpolator, boolean fillafter)
{
Animation translate = new TranslateAnimation(fromDelta.x, toDelta.x, fromDelta.y, toDelta.y);
if (null != context && 0 != interpolator)
{
translate.setInterpolator(AnimationUtils.loadInterpolator(context, interpolator));
}
translate.setFillAfter(fillafter);
translate.setDuration(duration);
return translate;
}
/**
* 기본 크기 애니메이션
*
* @since 1.0.0
* @param context
* @param from
* @param to
* @param duration
* @param interpolator
* @param fillafter
* @return the animation
*/
public static Animation scale(Context context, PointF from, PointF to, int duration, int interpolator, boolean fillafter)
{
Animation scale = new ScaleAnimation(from.x, to.x, from.y, to.y, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
if (null != context && 0 != interpolator)
{
scale.setInterpolator(AnimationUtils.loadInterpolator(context, interpolator));
}
scale.setFillAfter(fillafter);
scale.setDuration(duration);
return scale;
}
/**
* 기본 알파 애니메이션
*
* @since 1.0.0
* @param context
* @param from
* @param to
* @param duration
* @param interpolator
* @param fillafter
* @return the animation
*/
public static Animation alpha(Context context, float from, float to, int duration, int interpolator, boolean fillafter)
{
Animation scale = new AlphaAnimation(from, to);
if (null != context && 0 != interpolator)
{
scale.setInterpolator(AnimationUtils.loadInterpolator(context, interpolator));
}
scale.setFillAfter(fillafter);
scale.setDuration(duration);
return scale;
}
/**
* PathStyle 대메뉴 버튼 토글 애니메이션<br>
* 메뉴 버튼 45도 회전처리
*
* @since 1.0.0
* @param context
* @param menu
* @param duration
* @param onoff
*/
public static void performPathStyleMenu(Context context, View menu, int duration, boolean onoff)
{
Animation anim = null;
if (onoff)
{
anim = rotate(context, 0, 45, duration, android.R.anim.anticipate_overshoot_interpolator, true);
}
else
{
anim = rotate(context, -45, 0, duration, android.R.anim.anticipate_overshoot_interpolator, true);
}
menu.startAnimation(anim);
}
/**
* PathStyle 서브메뉴 버튼 애니메이션<br>
* 회전하면서 이동처리
*
* @since 1.0.0
* @param context
* @param subs
* 전체 서브메뉴 리스트
* @param index
* 해당 메뉴 인덱스
* @param length
* 서브메뉴가 펼쳐졌을 때의 길이
* @param duration
* 지속시간
* @param gap
* 각 버튼간 이동 시작 시간 offset
* @param onoff
*/
public static void performPathStyleSubMenu(Context context, ArrayList<AButton> subs, int index, int length, int duration, int gap, boolean onoff)
{
AButton view = subs.get(index);
float endX = length * FloatMath.cos((float) (Math.PI * 1 / 2 * (index) / (subs.size() - 1)));
float endY = length * FloatMath.sin((float) (Math.PI * 1 / 2 * (index) / (subs.size() - 1)));
AnimationSet animation = new AnimationSet(false);
Animation translate = null;
Animation rotate = rotate(context, 0, 360, duration, android.R.anim.accelerate_interpolator, false);
rotate.setRepeatCount(1);
if (onoff)
{
translate = translate(context, new PointF(0, 0), new PointF(endX, -endY), duration, android.R.anim.overshoot_interpolator, false);
translate.setStartOffset(gap * index);
view.setOffset(new PointF(endX, -endY));
}
else
{
translate = translate(context, new PointF(endX, -endY), new PointF(0, 0), duration, android.R.anim.anticipate_interpolator, false);
translate.setStartOffset(gap * (subs.size() - (index + 1)));
view.setOffset(new PointF(0, 0));
}
animation.setFillAfter(true);
animation.addAnimation(rotate);
animation.addAnimation(translate);
view.startAnimation(animation);
}
/**
* PathStyle 서브메뉴 버튼 선택 애니메이션<br>
* 선택된 메뉴는 점점 커지면서 투명해지다가 사라지고,<br>
* 나머지 메뉴는 점점 작아지면서 투명해지다가 사라지고, 대메뉴는 닫기 상태로 복귀한다.
*
* @since 1.0.0
* @param context
* @param menu
* @param subs
* @param selectIndex
* @param duration
*/
public static void performPathStyleSubMenuSelect(Context context, View menu, ArrayList<AButton> subs, int selectIndex, int duration)
{
for (int ii = 0; ii < subs.size(); ii++)
{
if (selectIndex == ii)
{
AButton view = subs.get(ii);
AnimationSet animation = new AnimationSet(false);
Animation translate = translate(context, new PointF(0, 0), view.getOffset(), 0, 0, false);
Animation scale = scale(context, new PointF(1.0f, 1.0f), new PointF(2.5f, 2.5f), duration, 0, false);
Animation alpha = alpha(context, 1.0f, 0.0f, duration, 0, false);
view.setOffset(new PointF(0, 0));
animation.addAnimation(scale);
animation.addAnimation(translate);
animation.addAnimation(alpha);
view.startAnimation(animation);
}
else
{
AButton view = subs.get(ii);
AnimationSet animation = new AnimationSet(false);
Animation translate = translate(context, new PointF(0, 0), view.getOffset(), 0, 0, false);
Animation scale = scale(context, new PointF(1.0f, 1.0f), new PointF(0.0f, 0.0f), duration, 0, false);
Animation alpha = alpha(context, 1.0f, 0.0f, duration, 0, false);
view.setOffset(new PointF(0, 0));
animation.addAnimation(scale);
animation.addAnimation(translate);
animation.addAnimation(alpha);
view.startAnimation(animation);
}
}
performPathStyleMenu(context, menu, duration, false);
}
/**
*
* @since 1.0.0
* @param parent
* @param fromView
* @param toView
* @param centerX
* @param centerY
*/
public static void performFlip(ViewGroup parent, View fromView, View toView, float centerX, float centerY)
{
FlipAnimator animator = new FlipAnimator(fromView, toView, centerX, centerY);
if (View.GONE == fromView.getVisibility())
{
animator.reverse();
}
parent.startAnimation(animator);
}
}