在画布上围绕枢轴旋转位图 [英] Rotating a bitmap around a pivot on a canvas

查看:114
本文介绍了在画布上围绕枢轴旋转位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个表盘表面,其中手的位图图像围绕画布上的表盘中心旋转.

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.

  1. 基本上是在onDraw()方法中,我希望能够将图像资源放到画布上,然后旋转它每秒旋转一次.
  2. 我有适当的逻辑可以每秒触发一次旋转,我不确定如何获取图像资源然后旋转它.

将提供任何帮助!

推荐答案

基本步骤:

  1. 例如从资源中解码位图,但是还有其他方法. (您希望在初始化类时执行一次操作,而不是在每个绘制周期上执行一次.):

  1. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆