简体   繁体   English

如何旋转合并 bitmap 而不会在侧面被切割/修剪

[英]How to rotate merge bitmap without being cutted/trim on the side

I merge bitmap using this code我使用此代码合并 bitmap

    Bitmap bitmapMerged = 
    Bitmap.createBitmap(
    w, h, bitmapOriginal.getConfig()); 

    Canvas canvasMerged = new  
    Canvas(bitmapMerged);            
    canvasMerged.drawBitmap
    (bitmapOriginal, 0, 0, null);

    canvasMerged.drawBitmap
    (bitmapSecond, 
    bitmapOriginal.getWidth(), 0, null);                 
    image3.setImageBitmap
   (bitmapMerged);

After I successfully merge the two bitmap (bitmapOriginal and bitmapSecond)在我成功合并两个bitmap(bitmapOriginal和bitmapSecond)之后

I want to rotate bitmapOriginal but after using this code我想旋转 bitmapOriginal 但在使用此代码后

     Matrix matrix = new Matrix(); 
    matrix.postRotate(20); 

     Bitmap scaledBitmap =  
    Bitmap.createScaledBitmap
    (bitmapOriginal, 
    bitmapOriginal.getWidth(), h, true); 
    Bitmap bitmapOriginal = 
    Bitmap.createBitmap
   (scaledBitmap, 0, 0, 
    scaledBitmap.getWidth(), 
   scaledBitmap.getHeight(), matrix, 
   true); 

bitmapOriginal rotated but it it side is cutted bitmapOriginal 旋转,但它的一侧被切割

How to fix this如何解决这个问题

By the way the value of w, and h.顺便说一下 w 和 h 的值。

  int w = bitmapOriginal.getWidth() + 
  bitmapSecond.getWidth();
  int h;
  if
 (bitmapOriginal.getHeight() >= 
 bitmapSecond.getHeight())
 { 
 h = bitmapOriginal.getHeight();
 }
 else
 { 
    h = bitmapSecond.getHeight(); 
 
 

 
    } 

点击查看图片

I believe your issue is with the value you are using for w and h.我相信您的问题在于您用于 w 和 h 的值。

You are setting the width to the sum of the widths of both bitmaps, and the height to the largest height of the two bitmaps.您将宽度设置为两个位图的宽度之和,将高度设置为两个位图的最大高度。 But if you think about it, if you rotate a rectangle, it's corners will be outside of where the original border was.但是如果你想一想,如果你旋转一个矩形,它的角将超出原始边框的位置。 See this image for a visual of what I mean.请参阅此图像以了解我的意思。

The solution for you would be to use larger values of w and h, to allow more space for the rotated corners.您的解决方案是使用较大的 w 和 h 值,以便为旋转的角留出更多空间。

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

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