android3d动画的实现,【转】Android 实现3d动画旋转效果

Roate3dAnimation 实现了围绕y轴竖直方向 或者绕x轴方向旋转的3d动画效果。这个例子来

自Android APIDemo中的一个自定义View动画。他的实现展示自定义View动画的基本步骤。

主要是重写initialize方法,applyTransformation方法。

分析:

在Roate3dAnimation中,我们使用Android.graphic.Camera实现3d效果。

对Camera不熟悉的可看看 android.graphic.Camera 。

public classRotate3dAnimationextendsAnimation {

//开始角度

private floatstartDegree;

//结束角度

private floatendDegree;

/**

* 这个旋转动画围绕在2D空间的中心点执行.你可以用X轴坐标(叫做centerX)和Y轴(叫做centerY)

* 坐标来定义这个中心点

*/

private floatcenterX;

private floatcenterY;

/**

* 控制镜头景深,不需要的话给0值即可

* mReverse 为true,表示反方向,false 表示正方向

*/

private floatdeepZ;

private booleanmReverse;

//用于辅助实现3d效果。

privateCameramCamera;

//X轴方向,或Y轴方向

enumDIRECTION {

X,Y

}

DIRECTIONdirection= DIRECTION.Y;

Rotate3dAnimation(floatfromDegree,floattoDegree,floatcenterX,

floatcenterY,floatdeepZ,booleanreverse) {

this.startDegree= fromDegree;

this.endDegree= toDegree;

this.centerX= centerX;

this.centerY= centerY;

this.deepZ= deepZ;

this.mReverse= reverse;

}

Rotate3dAnimation(floatfromDegree,floattoDegree,floatcenterX,

floatcenterY,floatdeepZ,booleanreverse, DIRECTION direction) {

this.startDegree= fromDegree;

this.endDegree= toDegree;

this.centerX= centerX;

this.centerY= centerY;

this.deepZ= deepZ;

this.mReverse= reverse;

this.direction= direction;

}

@Override

public void initialize(int width, int height, int parentWidth, int parentHeight) {

super.initialize(width, height, parentWidth, parentHeight);

mCamera=newCamera();

}

@Override

protected void applyTransformation( float int erpolatedTime, Transformation t) {

super.applyTransformation(interpolatedTime, t);

float fromDegree =startDegree;

float degree = fromDegree + (endDegree-startDegree) * interpolatedTime;

finalMatrix matrix = t.getMatrix();

mCamera.save();

if(mReverse) {

mCamera.translate(0, 0, deepZ* interpolatedTime);

}else{

mCamera.translate(0, 0, deepZ* (1- interpolatedTime));

}

if(direction== DIRECTION.Y) {

mCamera.rotateY(degree);

}else{

mCamera.rotateX(degree);

}

mCamera.getMatrix(matrix);

mCamera.restore();

matrix.preTranslate(-centerX, -centerY);

matrix.postTranslate(centerX,centerY);

}

}

应用:

iv_content.post(new Runnable() {

@Override

public void run() {

Rotate3dAnimation rotate3dAnimation = new Rotate3dAnimation(0, 360, iv_content.getWidth()/2,

0, 0, true, Rotate3dAnimation.DIRECTION.Y);

rotate3dAnimation.setDuration(3000);

iv_content.setAnimation(rotate3dAnimation);

rotate3dAnimation.start();

}

});

原文链接:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值