简体   繁体   English

在Android中旋转图像的问题

[英]Problem in rotating image in android

I am facing problme in rotating image 我在旋转图像时面临问题

Following code works fine 以下代码可以正常工作

Matrix matrix = new Matrix();
matrix.postRotate(DEGREE,mBitmap.getWidth() / 2, mBitmap.getHeight() / 2);
Bitmap m = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(),mBitmap.getHeight(), matrix, true);
canvas.drawBitmap(m, mX, mY, null);

But I dont want to create a new bitmap again and again so I am using the following code 但是我不想一次又一次地创建一个新的位图,所以我正在使用以下代码

Matrix matrix = new Matrix();
matrix.postTranslate(mX, mY);
matrix.postRotate(DEGREE,mBitmap.getWidth() / 2, mBitmap.getHeight() / 2);
canvas.drawBitmap(mBitmap, matrix, null);

In that case image goes out of the view. 在这种情况下,图像将无法显示。 It is not visible. 它是不可见的。

Matrix matrix = new Matrix();
canvas.translate(mX, mY);
canvas.drawBitmap(...);
canvas.translate(-mX, -mY);

尝试先旋转它,然后再平移它,因为当您首先平移它时,中心不再在中间,因此您使用错误的枢轴坐标旋转它。

The pivot point by default when rotating is the top left corner of the image, which is why the view goes out of view. 默认情况下,旋转时的枢轴点是图像的左上角,这就是视图不可见的原因。 You need to add logic to make the pivot point the center of the image. 您需要添加逻辑以使枢轴点位于图像的中心。 Unfortunately, geometry is not my strong suit so maybe someone who somewhat enjoys geometry can give you the calculations to make this happen. 不幸的是,几何不是我的强项,所以也许有些喜欢几何的人可以为您提供计算方法以实现这一目标。

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

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