原理
a 3×3 affine transformation.
这里我们使用 Android.graphics.Camera 的 rotateY 接口实现绕 Y 轴旋转时矩阵的运算。
实现
基本流程如下所示:
- Get canvas Matrix m: Matrix m = canvas.getMatrix ();
- Camera 绕 Y 轴旋转 angle 角度: camera.rotateY (angle);
- 将 Camera 作用于 m: camera.getMatrix (m);
- 将矩阵 m 应用于 canvas : canvas.setMatrix (m);
- 绘制: canvas.drawXXX.
示例代码如下所示:
/* double buffer: mCanvas/mBitmap */ Bitmap mBitmap = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888); Canvas mCanvas = new Canvas (mBitmap); Paint mPaint = new Paint (); Camera mCamera = new Camera(); void onDraw(Canvas canvas) { mBitmap.eraseColor (0xff000000); mCanvas.setBitmap(mBitmap); Matrix m = mCanvas.getMatrix (); mCamera.save (); mCamera.rotateY (angle); mCamera.getMatrix (m); mCamera.restore (); mCanvas.setMatrix (m); mCanvas.drawText ("5", x0, y0, mPaint); canvas.drawBitmap(mBitmap, x1, y1, mPaint); }
效果图如下所示: