安卓没有3d的动画效果,需要重写动画类,下面的代码来自网络,我做了一下注释。
package com.example.testbuttonanimation;
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 Rotate3dAnimation extends Animation {
//3d旋转起始角度
private final float mFromDegrees;
//结束角度
private final float mToDegrees;
//3d旋转xyz中心
private final float mCenterX;
private final float mCenterY;
private final float mDepthZ;
//按照代码ture为正方向
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
*/
// 构造函数!!
//第一个参数是起始角度
//第二个参数是最终角度
//第三个参数是旋转时候,是以x轴的什么位置,如果让这个值(和四个参数同时)等于0的话就是以最左边,如果让其等于图片宽的一半(第四个参数等于高的一半),就是以中心轴旋转
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;
}
// 重写的父类初始化方法
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
}
//重写的父类applyTransformation方法,该方法就是动画具体的实现,动画执行的时候会调用这个方法,可以通过重写这个方法来自定义自己的动画
@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) {
//三个参数分别为xyz的偏移量,这里只有z偏移
camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
} else {
camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
}
//偏移的同时旋转
camera.rotateY(degrees);
//camera.rotateX(degrees);
//变换应用到变换矩阵,调用完,将camera位置恢复,以便下一次再使用。
camera.getMatrix(matrix);
//恢复位置
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}
具体的参数我已经都注释好了
调用的时候只需要传入参数即可
Rotate3dAnimation rotation = new Rotate3dAnimation(-40, 0, findViewById(R.id.button1).getWidth()/2,findViewById(R.id.button1).getHeight()/2, 0.0f, false);
rotation.setDuration(2000);
b.startAnimation(rotation);
禁止转载