package com.duguang.baseanimation.utils;

import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.RotateAnimation;

public class Utils {
	
//	private static boolean isInRunning = false;
//	private static boolean isOutRunning = false;
	private static boolean isRunning = false;
	

	/**
	 * ִ�н���Ķ���
	 */
	public static void startInRotateAnimation(ViewGroup viewGroup, long startOffset) {
		// ��viewGroup�еĿؼ���Ϊ����
		for (int i = 0; i < viewGroup.getChildCount(); i++) {
			viewGroup.getChildAt(i).setEnabled(true);		// �õ�viewGroup�е��Ӳ���, ��Ϊ����
		}
		
		RotateAnimation anim = new RotateAnimation(
				-180f, 0f, 
				Animation.RELATIVE_TO_SELF, 0.5f, 	// x������Լ���ȵ�һ��
				Animation.RELATIVE_TO_SELF, 1.0f);	// y������Լ��߶ȵ�һ��
		anim.setDuration(500);			// ����ִ��ʱ��
		anim.setStartOffset(startOffset);		// ������ʱʱ��
		anim.setFillAfter(true);		// ��viewͣ���ڶ���ֹͣ��λ����
		anim.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				isRunning = true;
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				isRunning = false;
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}});
		viewGroup.startAnimation(anim);
	}
	
	/**
	 * ִ�г�ȥ�Ķ���
	 * @param viewGroup
	 * @param startOffset ��ʱʱ��
	 */
	public static void startOutRotateAnimation(ViewGroup viewGroup, long startOffset) {
		// ��viewGroup�еĿؼ���Ϊ������
		for (int i = 0; i < viewGroup.getChildCount(); i++) {
			viewGroup.getChildAt(i).setEnabled(false);		// �õ�viewGroup�е��Ӳ���, ��Ϊ������
		}
		
		RotateAnimation anim = new RotateAnimation(
				0f, -180f, 
				Animation.RELATIVE_TO_SELF, 0.5f, 	// x������Լ���ȵ�һ��
				Animation.RELATIVE_TO_SELF, 1.0f);	// y������Լ��߶ȵ�һ��
		anim.setDuration(500);			// ����ִ��ʱ��
		anim.setStartOffset(startOffset);		// ������ʱʱ��
		anim.setFillAfter(true);		// ��viewͣ���ڶ���ֹͣ��λ����
		
		anim.setAnimationListener(new AnimationListener() {

			/**
			 * ������ʼʱ�ص�
			 */
			@Override
			public void onAnimationStart(Animation animation) {
				isRunning = true;
			}

			/**
			 * ��������ʱ�ص�
			 */
			@Override
			public void onAnimationEnd(Animation animation) {
				isRunning = false;
			}

			/**
			 * �����ظ�ִ��ʱ�ص�
			 */
			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}});
		viewGroup.startAnimation(anim);
	}
	

	/**
	 * �ж��Ƿ�����ִ�ж�����
	 * @return
	 */
	public static boolean isRunningAnimation() {
//		return isInRunning || isOutRunning;
		return isRunning;
	}
}
