회전하려는 영역의 좌표와 크기를 알고 있거나 이해할 수 있으면 비교적 간단합니다.
- 이미지를 변경할 수있는
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 형식으로 이미지를 저장합니다.
모두 하나의 이미지이거나 4 개의 별도 이미지입니까? – chornge
이것이 모두 하나의 이미지라면, 그냥 그려 보는 것이 더 쉬울 것입니다. –
@chornge 아니, 모두 하나의 이미지입니다. – Daniele