android 动画x轴旋转,Android Roate3dAnimation实现围绕y轴竖直方向或者绕x轴方向旋转的3d动画效果...
概要:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Roate3dAnimation 實現了圍繞y軸豎直方向 或者繞x軸方向旋轉的3d動畫效果。這個例子來
自Android APIDemo中的一個自定義View動畫。他的實現展示自定義View動畫的基本步驟。
主要是重寫initialize方法,applyTransformation方法。
分析:
在Roate3dAnimation中,我們使用Android.graphic.Camera實現3d效果。
對Camera不熟悉的可看看?android.graphic.Camera?。
public?class?Rotate3dAnimation?extends?Animation?{
//開始角度
private?float?startDegree;
//結束角度
private?float?endDegree;
/**
*?這個旋轉動畫圍繞在2D空間的中心點執行.你可以用X軸坐標(叫做centerX)和Y軸(叫做centerY)
*?坐標來定義這個中心點
*/
private?float?centerX;
private?float?centerY;
/**
*?控制鏡頭景深,不需要的話給0值即可
*?mReverse?為true,表示反方向,false?表示正方向
*/
private?float?deepZ;
private?boolean?mReverse;
//用于輔助實現3d效果。
private?Camera?mCamera;
//X軸方向,或Y軸方向
enum?DIRECTION?{
X,?Y
}
DIRECTION?direction?=?DIRECTION.Y;
Rotate3dAnimation(float?fromDegree,?float?toDegree,?float?centerX,
float?centerY,?float?deepZ,?boolean?reverse)?{
this.startDegree?=?fromDegree;
this.endDegree?=?toDegree;
this.centerX?=?centerX;
this.centerY?=?centerY;
this.deepZ?=?deepZ;
this.mReverse?=?reverse;
}
Rotate3dAnimation(float?fromDegree,?float?toDegree,?float?centerX,
float?centerY,?float?deepZ,?boolean?reverse,?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?=?new?Camera();
}
@Override
protected?void?applyTransformation(float?interpolatedTime,?Transformation?t)?{
super.applyTransformation(interpolatedTime,?t);
float?fromDegree?=?startDegree;
float?degree?=?fromDegree?+?(endDegree?-?startDegree)?*?interpolatedTime;
final?Matrix?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();
}
});
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
小奮斗文章
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
總結
以上是生活随笔為你收集整理的android 动画x轴旋转,Android Roate3dAnimation实现围绕y轴竖直方向或者绕x轴方向旋转的3d动画效果...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求一个五十岁男人微信网名
- 下一篇: android 8.0可以实现后台包活么