十一前请了一天假,后面请了三天假,串起来一共休息了十二天,这期间见了女朋友的家长,也领女朋友见了我家长,好消息是各方家长反馈不错,坏消息是涨了五斤肉,毕竟是吃了12天的呼伦贝儿牛羊肉哈。玩了这么多天,回来要收收心好好工作了,毕竟要攒钱娶媳妇哈~
上班回来,按照产品的需求做了这样一个效果,如下图:
这其中包含了3个动画,分别是移动动画、放大动画、旋转动画。
前两个动画比较好完成,可以用 TranslateAnimation 和 ScaleAnimation来完成,但是第三个就比较麻烦了,因为这个旋转动画是根据 Y 轴来旋转的,而 RotateAnimation 是根据垂直屏幕的 Z 轴旋转的,所以 RotateAnimation 并不能够完成我们需要的效果,这里就需要我们去继承 Animation 去自定义动画了。
但是!!!
但是 APIdemo 里面已经实现了这个动画,so,我们拿来用就好了,下面是我贴出来的Rotate3dAnimation,其中我添加了一个构造,添加了相对自己位置的 type。
package cn.kenneth.rotate3danimation;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;
public class Rotate3dAnimation extends Animation {
private int mCenterXType = ABSOLUTE;
private int mCenterYType = ABSOLUTE;
private final float mFromDegrees;
private final float mToDegrees;
private float mCenterX;
private float mCenterY;
private final float mDepthZ;
private final boolean mReverse;
private Camera mCamera;
/**
* 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 Rotate3dAnimation(float fromDegrees, float toDegrees,
float centerX, float centerY, float depthZ, boolean reverse) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mCenterX = centerX;
mCenterY = centerY;
mDepthZ = depthZ;
mReverse = reverse;
}
public Rotate3dAnimation(float fromDegrees, float toDegrees,
int centerXType, float centerX, int centerYType, float centerY, float depthZ, boolean reverse) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mCenterXType = centerXType;
mCenterX = centerX;
mCenterYType = centerYType;
mCenterY = centerY;
mDepthZ = depthZ;
mReverse = reverse;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
mCenterX = resolveSize(mCenterXType, mCenterX, width, parentWidth);
mCenterY = resolveSize(mCenterYType, mCenterY, height, parentHeight);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save();
if (mReverse) {
camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
} else {
camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
}
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}
移动动画也比较好做,点击的时候记录点击 icon 在 window 上的位置,然后传到下一个界面,计算一下偏移量。