原文链接 : 一个炫字都不够??!!!手把手带你打造3D自定义view
在此学习了一下 并分析了一下API Demo 中的3d动画类
首先继承一个View并绘出一个圆(控件加了一个背景颜色)
![]()
<code>ondraw方法中重写
centerX = getMeasuredWidth()/2.0f;
centerY = getMeasuredHeight()/2.0f;
canvas.drawCircle(centerX, centerY, 100, mPaint);
</code>
- 然后使用Camera类先了解一下旋转效果
<code>
同样onDraw方法中加入 代码
//初始化单位矩阵
matrix.reset();
//camera保存状态,放入相应的栈中
camera.save();
//设置 绕x轴旋转度数
camera.rotateX(10f );
//设置绕y走旋转度数
camera.rotateY(20f );
//把camera的初始的matrix设置给matrix
camera.getMatrix(matrix);
//camera恢复保存的状态
camera.restore();
//总体效果是 以圆心为中心旋转
//在绘制前移动
matrix.preTranslate(-centerX, -centerY);
//在设置后移动
matrix.postTranslate(centerX, centerY);
//把canvas的对象与matrix联系
canvas.concat(matrix);
</code>
![](https://img.haomeiwen.com/i1708768/10211e58a2692f79.png)
-然后就剩下处理touch事件了
<code>
//处理触摸事件
@Override
public boolean onTouchEvent(MotionEvent event) {
float vX = event.getX();
float vY = event.getY();
if(event.getAction() == MotionEvent.ACTION_MOVE){
//限制旋转度数
rotateCanvasWhenMove(vX,vY);
//重画
invalidate();
}else if(event.getAction() == MotionEvent.ACTION_UP){
mCanvasRotateX = 0;
mCanvasRotateY = 0;
//重画
invalidate();
}
//此处返回true 否则不能一直处理touch事件
return true;
}
</code>
网友评论