/**
 * 文件名：RotateAnim.java
 * 描述：RotateAnim工具类
 * 包名：cn.com.kmlt.monstarBase.utils.image
 * 作者：WENJQ
 * 创建日期：2016年05月26日
 * 
 */
package com.mgq.base.utils.image;



import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;

/**
 * An animation that rotates the view on the Y axis between two specified angles.
 * This animation also adds a translation on the Z axis (depth) to improve the effect.
 */
public class RotateAnim extends Animation {
    private final float mCenterX;
    private final float mCenterY;
    private final float mDepthZ;
    private final boolean mReverse;
    private final int myPositon;
    private Camera mCamera;
    private InterpolatedTimeListener listener;

    /**
     * Creates a new 3D rotation on the Y axis. The rotation is defined by its
     * start angle and its end angle. Both angles are in degrees. The rotation
     * is performed around a center point on the 2D space, definied by a pair
     * of X and Y coordinates, called centerX and centerY. When the animation
     * starts, a translation on the Z axis (depth) is performed. The length
     * of the translation can be specified, as well as whether the translation
     * should be reversed in time.
     *
     * @param fromDegrees the start angle of the 3D rotation
     * @param toDegrees the end angle of the 3D rotation
     * @param centerX the X center of the 3D rotation
     * @param centerY the Y center of the 3D rotation
     * @param reverse true if the translation should be reversed, false otherwise
     */
    public RotateAnim(float centerX, float centerY, float depthZ, boolean reverse,int positon) {
           
        mCenterX = centerX;
        mCenterY = centerY;
        mDepthZ = depthZ;
        mReverse = reverse;
        myPositon = positon;
    }

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
        mCamera = new Camera();
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
    	if (listener != null) {
			listener.interpolatedTime(interpolatedTime);
		}
    	final boolean type = mReverse;
    	final float from;
    	final float  to;
    	if(type){
    		from = 0.0f;
			to = 180.0f;
    	}else{
    		from = 360.0f;
			to = 180.0f;
    	}   	 	
        float degrees = from + ((to - from) * interpolatedTime);
    	//float degrees = from + (to - from) * interpolatedTime;
    	final float depth = (0.5f - Math.abs(interpolatedTime - 0.5f)) * 310.0F;
    	
        final float centerX = mCenterX;
        final float centerY = mCenterY;
        final Camera camera = mCamera;
        boolean overHalf = (interpolatedTime >= 0.5f);
		if (overHalf) {
			// 翻转过半的情况下，为保证数字仍为可读的文字而非镜面效果的文字，需翻转180度。
			degrees = degrees - 180;
		}
        final Matrix matrix = t.getMatrix();

        camera.save();      
        camera.translate(0.0f, 0.0f, depth);                      
        camera.rotateY(degrees);
        camera.getMatrix(matrix);
        camera.restore();
        
        matrix.preTranslate(-centerX, -centerY);
        matrix.postTranslate(centerX, centerY);
    }
    
	/** 用于监听动画进度。当值过半时需更新的内容。 */	

	public void setInterpolatedTimeListener(InterpolatedTimeListener listener) {
		this.listener = listener;
	}

	/** 动画进度监听器。 */
	public static interface InterpolatedTimeListener {
		public void interpolatedTime(float interpolatedTime);
	}
}
