在画布上围绕枢轴旋转位图 [英] Rotating a bitmap around a pivot on a canvas
问题描述
我正在尝试实现一个表盘表面,其中手的位图图像围绕画布上的表盘中心旋转.
I am trying to implement a watch face where a bitmap image of a hand rotates about the center of the face on a canvas.
- 基本上是在onDraw()方法中,我希望能够将图像资源放到画布上,然后旋转它每秒旋转一次.
- 我有适当的逻辑可以每秒触发一次旋转,我不确定如何获取图像资源然后旋转它.
将提供任何帮助!
推荐答案
基本步骤:
-
例如从资源中解码位图,但是还有其他方法. (您希望在初始化类时执行一次操作,而不是在每个绘制周期上执行一次.):
Decode your bitmap from, for example, Resources, but there are other ways. (You would want to do this once when initializing the class, not on every draw cycle.):
Bitmap = mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.watch_face);
在onDraw
内设置一个Matrix
进行旋转.像这样:
Set up a Matrix
inside onDraw
for the rotation. Something like:
Matrix mMatrix = new Matrix();
mMatrix.setRotate (degreeOfRotation, mBitmap.getWidth()/2, mBitmap.getHeight()/2);
Matrix mMatrix = new Matrix();
mMatrix.setRotate (degreeOfRotation, mBitmap.getWidth()/2, mBitmap.getHeight()/2);
使用采用矩阵的方法绘制位图:
canvas.drawBitmap(mBitmap, mMatrix, mPaint);
Draw the Bitmap using a method that takes in your Matrix:
canvas.drawBitmap(mBitmap, mMatrix, mPaint);
还有其他详细信息.设置Paint
.您可能会发现您需要使用Matrix的postRotate
方法.等等,但这足以让您开始使用Google搜索.
There are other details. Setting up Paint
. You may find you need to use the Matrix's postRotate
method instead. Etc. Etc. But this should be enough to you started Googling.
这篇关于在画布上围绕枢轴旋转位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!