简体   繁体   English

围绕中心旋转图像

[英]Rotate an image around center

How can I rotate an image around it's center point? 如何围绕它的中心点旋转图像? This rotates it but also moves it: 这会旋转它但也移动它:

 Matrix mat = new Matrix();
 mat.postRotate(45);
 Bitmap bMapRotate = Bitmap.createBitmap(dialBM, 0, 0, dialBM.getWidth(),dialBM.getHeight(), mat, true);
dial.setImageBitmap(bMapRotate);

I've checked other examples on this site, but they either fail to work or use canvas, I do not wish to use canvas. 我已经检查了这个网站上的其他例子,但他们要么无法工作,要么使用画布,我不想使用画布。

postRotate的第二个和第三个参数是x和y轴点。

 mat.postRotate(45, dialBM.getWidth()/2, dialBM.getHeight()/2);

Probably because your matrix rotates around (0,0), and not the middle of your bitmap. 可能是因为矩阵围绕(0,0)旋转,而不是位图的中间位置。 You should declare two additional matrices - one for moving the bitmap's center to (0,0) (shift by -getWidth()/2, -getHeight(2)) and one to move the bitmap's center back to (0,0). 你应该声明两个额外的矩阵 - 一个用于将位图的中心移动到(0,0)(移动-getWidth()/ 2,-getHeight(2)),另一个用于将位图的中心移回(0,0)。 Multiply the three matrices, and then the result. 将三个矩阵相乘,然后乘以结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM