的Android - 旋转图像
问题描述:
我基本上需要旋转90个degres一个ImageView
的一小部分(例如)的一部分:的Android - 旋转图像
在上面的图片,我想旋转4所以它显示正确。只有4个,其余的应该保持垂直。
有没有办法实现它?
通过实施MikeM建议的方法。我收到以下结果。
正如你可以看到有两个主要的事情,我需要解决:
- 旋转后的广场工作,虽然在拧位置。如何找出
4
的准确坐标 - 图像的背景已被更改为黑色。它曾经是透明
答
如果您知道,或者自己看着办,你想要的坐标和区域的尺寸,旋转,那么这个过程是相对简单的。
- 将图像加载为可变的
Bitmap
。 - 从原始创建第二个旋转的
Bitmap
所需区域。 - 在原始
Bitmap
上创建一个Canvas
。 - 如有必要,清除修剪区域。
- 将旋转的区域绘制回原件上。
在以下示例中,它假定该区域的坐标(x
,y
)和尺寸(width
,height
)是已知的。
// Options necessary to create a mutable Bitmap from the decode
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
// Load the Bitmap, here from a resource drawable
Bitmap bmp = BitmapFactory.decodeResource(getResources(), resId, options);
// Create a Matrix for 90° counterclockwise rotation
Matrix matrix = new Matrix();
matrix.postRotate(-90);
// Create a rotated Bitmap from the desired region of the original
Bitmap region = Bitmap.createBitmap(bmp, x, y, width, height, matrix, false);
// Create our Canvas on the original Bitmap
Canvas canvas = new Canvas(bmp);
// Create a Paint to clear the clipped region to transparent
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
// Clear the region
canvas.drawRect(x, y, x + width, y + height, paint);
// Draw the rotated Bitmap back to the original,
// concentric with the region's original coordinates
canvas.drawBitmap(region, x + width/2f - height/2f, y + height/2f - width/2f, null);
// Cleanup the secondary Bitmap
region.recycle();
// The resulting image is in bmp
imageView.setImageBitmap(bmp);
为了解决关注在编辑:
旋转后的区域在原始示例的数字是基于与所述长轴垂直在图像上。编辑中的图像在之后已被旋转至垂直,该区域已被修改。
黑色背景是由于已将结果图像插入到
MediaStore
中,该图像以不支持透明度的JPEG格式保存图像。
+1
再次感谢您提供的巨大帮助。 – Daniele
这是全部图像还是4个独立的图像? – chornge
如果这只是一个单一的图像,它可能会更容易画出来。 –
@chornge不,它是一个图像。 – Daniele