我基本上需要旋轉90個degres一個ImageView
的一小部分(例如)的一部分:的Android - 旋轉圖像
在上面的圖片,我想旋轉4所以它顯示正確。只有4個,其餘的應該保持垂直。
有沒有辦法實現它?
通過實施MikeM建議的方法。我收到以下結果。
正如你可以看到有兩個主要的事情,我需要解決:
- 旋轉後的廣場工作,雖然在擰位置。如何找出
4
的準確座標 - 圖像的背景已被更改爲黑色。它曾經是透明
我基本上需要旋轉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格式保存圖像。
再次感謝您提供的巨大幫助。 – Daniele
這是全部圖像還是4個獨立的圖像? – chornge
如果這只是一個單一的圖像,它可能會更容易畫出來。 –
@chornge不,它是一個圖像。 – Daniele