package net.card7.view.anim;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;
public class RotateAnimation extends Animation {
/** 值为true时可明确查看动画的旋转方向。 */
public static final boolean DEBUG = false;
/** 沿Y轴正方向看,数值减1时动画逆时针旋转。 */
public static final boolean ROTATE_DECREASE = true;
/** 沿Y轴正方向看,数值减1时动画顺时针旋转。 */
public static final boolean ROTATE_INCREASE = false;
/** Z轴上最大深度。 */
public static final float DEPTH_Z = 310.0f;
/** 动画显示时长。 */
public static final long DURATION = 800l;
/** 图片翻转类型。 */
private final boolean type;
private final float centerX;
private final float centerY;
private Camera camera;
public RotateAnimation(float cX, float cY, boolean type) {
centerX = cX;
centerY = cY;
this.type = type;
// 设置动画时长
setDuration(DURATION);
}
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
// 在构造函数之后、applyTransformation()之前调用本方法。
super.initialize(width, height, parentWidth, parentHeight);
camera = new Camera();
}
@Override
protected void applyTransformation(float interpolatedTime,
Transformation transformation) {
// interpolatedTime:动画进度值,范围为0~1,0.5为正好翻转一半
if (listener != null) {
listener.interpolatedTime(interpolatedTime);
}
float from = 0.0f, to = 0.0f;
if (type == ROTATE_DECREASE) {
from = 0.0f;
to = 180.0f;
} else if (type == ROTATE_INCREASE) {
from = 360.0f;
to = 180.0f;
}
// 旋转的角度
float degree = from + (to - from) * interpolatedTime;
boolean overHalf = (interpolatedTime > 0.5f);
if (overHalf) {
degree = degree - 180;
}
// 旋转深度
float depth = (0.5f - Math.abs(interpolatedTime - 0.5f)) * DEPTH_Z;
final Matrix matrix = transformation.getMatrix();
camera.save();
camera.translate(0.0f, 0.0f, depth);
camera.rotateY(degree);
camera.getMatrix(matrix);
camera.restore();
if (DEBUG) {
if (overHalf) {
matrix.preTranslate(-centerX * 2, -centerY);
matrix.postTranslate(centerX * 2, centerY);
}
} else {
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}
/** 用于监听动画进度。当值过半时需更新的内容。 */
private InterpolatedTimeListener listener;
public void setInterpolatedTimeListener(InterpolatedTimeListener listener) {
this.listener = listener;
}
/** 动画进度监听器。 */
public static interface InterpolatedTimeListener {
public void interpolatedTime(float interpolatedTime);
}
}
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;
public class RotateAnimation extends Animation {
/** 值为true时可明确查看动画的旋转方向。 */
public static final boolean DEBUG = false;
/** 沿Y轴正方向看,数值减1时动画逆时针旋转。 */
public static final boolean ROTATE_DECREASE = true;
/** 沿Y轴正方向看,数值减1时动画顺时针旋转。 */
public static final boolean ROTATE_INCREASE = false;
/** Z轴上最大深度。 */
public static final float DEPTH_Z = 310.0f;
/** 动画显示时长。 */
public static final long DURATION = 800l;
/** 图片翻转类型。 */
private final boolean type;
private final float centerX;
private final float centerY;
private Camera camera;
public RotateAnimation(float cX, float cY, boolean type) {
centerX = cX;
centerY = cY;
this.type = type;
// 设置动画时长
setDuration(DURATION);
}
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
// 在构造函数之后、applyTransformation()之前调用本方法。
super.initialize(width, height, parentWidth, parentHeight);
camera = new Camera();
}
@Override
protected void applyTransformation(float interpolatedTime,
Transformation transformation) {
// interpolatedTime:动画进度值,范围为0~1,0.5为正好翻转一半
if (listener != null) {
listener.interpolatedTime(interpolatedTime);
}
float from = 0.0f, to = 0.0f;
if (type == ROTATE_DECREASE) {
from = 0.0f;
to = 180.0f;
} else if (type == ROTATE_INCREASE) {
from = 360.0f;
to = 180.0f;
}
// 旋转的角度
float degree = from + (to - from) * interpolatedTime;
boolean overHalf = (interpolatedTime > 0.5f);
if (overHalf) {
degree = degree - 180;
}
// 旋转深度
float depth = (0.5f - Math.abs(interpolatedTime - 0.5f)) * DEPTH_Z;
final Matrix matrix = transformation.getMatrix();
camera.save();
camera.translate(0.0f, 0.0f, depth);
camera.rotateY(degree);
camera.getMatrix(matrix);
camera.restore();
if (DEBUG) {
if (overHalf) {
matrix.preTranslate(-centerX * 2, -centerY);
matrix.postTranslate(centerX * 2, centerY);
}
} else {
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}
/** 用于监听动画进度。当值过半时需更新的内容。 */
private InterpolatedTimeListener listener;
public void setInterpolatedTimeListener(InterpolatedTimeListener listener) {
this.listener = listener;
}
/** 动画进度监听器。 */
public static interface InterpolatedTimeListener {
public void interpolatedTime(float interpolatedTime);
}
}