如何找到用矩阵旋转和缩放的位图的中心位置? [英] How to find center position of a bitmap that rotated and scaled with a matrix?

查看:229
本文介绍了如何找到用矩阵旋转和缩放的位图的中心位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算在具有矩阵的画布上绘制的位图的中心,可以使用任意值对其进行旋转,缩放或平移.在画布上查找此位图中心的最简单方法是什么?

I want to calculate center of a bitmap that is drawn on a canvas with a matrix, it can be rotated, scaled or translated with a arbitrary value. What is the easiest way to find center of the this bitmap on canvas?

推荐答案

您需要将矩阵应用于位图中心的坐标.

You need to apply the matrix to the coordinates of the center of bitmap.

如果您使用具有转换矩阵的画布,则可以通过

If you use a canvas that has a transformation matrix, you can get the final matrix through Canvas.getMatrix()

如果使用矩阵drawBitmap(bitmap, matrix, paint)在画布上绘制位图,则需要使用该矩阵,或将其连接到画布的矩阵(如果有的话).

If you draw the Bitmap on the Canvas with a Matrix : drawBitmap(bitmap, matrix, paint), then you you need to use that Matrix, or concatenate it to that of the Canvas (in the case it has one).

然后,您最终可以使用 Matrix.mapPoints .

Then you can finally apply that matrix to the center of the matrix using Matrix.mapPoints.

类似的东西:

canvas.drawBitmap(bitmap, bitmap_transform, paint);
Matrix full_transform = new Matrix(canvas.getMatrix());
full_transform.postConcat(bitmap_transform);
float[] center = new float[] {bitmap.getHeight()/2.0, bitmap.getWidth()/2.0};
full_transform.mapPoints(center);

或者,如果将转换应用于没有矩阵的位图,则可以使用具有相同值的full_transform.postRotatefull_transform.postScale等.特别是,如果使用drawBitmap(bitmap, left, top, paint)绘制位图,则需要执行full_transform.postTranslate(left, top).

Alternatively, if you apply transformations to your bitmap without a matrix, you can use the full_transform.postRotate, full_transform.postScale, etc with the same values. In particular, if you draw your bitmap with drawBitmap(bitmap, left, top, paint) then you need to do a full_transform.postTranslate(left, top).

这篇关于如何找到用矩阵旋转和缩放的位图的中心位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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