繁体   English   中英

Android-旋转图像的一部分

[英]Android - Rotate a part of an Image

基本上,我需要旋转90个degres的一小部分ImageView (例如):

例

在上面的图像中,我想旋转4以使其正确显示。 只有4个,其余的应该保持垂直。

有办法可以实现吗?

通过实现MikeM建议的方法。 我得到以下结果。

结果

如您所见,我需要修复两个主要问题:

  1. 尽管处于拧紧位置,旋转的正方形仍在工作。 我如何找出4的确切坐标
  2. 图像的背景已更改为黑色。 过去是透明的

如果您知道或可以算出您想旋转的区域的坐标和尺寸,则该过程相对简单。

  1. 将图像加载为可变Bitmap
  2. 从原始文件创建第二个旋转的所需区域的Bitmap
  3. 在原始Bitmap上创建Canvas
  4. 如有必要,清除剪切区域。
  5. 将旋转的区域拖回原件上。

在下面的示例中,假定区域的坐标( xy )和尺寸( widthheight )是已知的。

// 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);

要解决编辑中的问题:

  1. 原始示例中旋转区域的图形基于长轴垂直的图像。 修改该区域 ,编辑中的图像已旋转到垂直方向。

  2. 黑色背景是由于将生成的图像插入MediaStore所致,后者将图像保存为JPEG格式,不支持透明性。

暂无
暂无

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

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