文章转载只能用于非商业性质,且不能带有虚拟货币、积分、注册等附加条件。转载须注明出处http://blog.csdn.net/flowingflying以及作者@恺风Wei。
利用Camera类可以为View的2D动画提供更好的视觉效果,并能在一定程度上模仿简单的3D效果。下面,我们利用Camera类实现View从原至近(z轴)不断放大,同时围绕Y轴旋转的效果。
小例子的代码片段
public class ViewAnimationCamera extends Animation{
private float centerX,centerY;
private Camera camera = null;
public ViewAnimationCamera(){
camera = new Camera();
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
centerX = width/2.0f;
centerY = height/2.0f;
setDuration(2500);
setFillAfter(true);
setInterpolator(new LinearInterpolator());
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final Matrix matrix = t.getMatrix();
//保存状态,与restore()成对出现
camera.save();
//三个参数分别代表x,y,z的坐标,随着时间,从(0,0,1300)向(0,0,0) 变化,实现从远到近的效果。对于z轴(景深)变化,已经提供了透视的效果,无需scale设置。
camera.translate(0.0f, 0.0f, 1300 * (1.0f- interpolatedTime));
//围绕Y轴作一周(360度)的旋转
camera.rotateY(360*interpolatedTime);
//将camera产生的效果加诸于参数matrix上
camera.getMatrix(matrix);
//下面两行代码,标识动画中心点是中央,前面已经详细讨论过。
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
//在使用camera设置动画之前,进行了camera.save(),将状态恢复到保存的状态。
camera.restore();
}
}
小例子代码在:Pro Android学习:2D动画小例子
相关链接:我的Android开发相关文章