2017-10-18 1 views
1

이미지의 일부 회전 : 위의 이미지에서안드로이드가 기본적으로 내가 90 degres 작은 <code>ImageView</code>의 일부 (예를 들어) 회전 할 필요가

example

을, 나는 4를 회전하고 싶습니다 그래서 올바르게 표시됩니다. 4 명만 나머지는 그대로 수직으로 유지해야합니다.

내가 이룰 수있는 방법이 있습니까?

MikeM이 제안한 방법을 구현합니다. 다음 결과가 나타납니다.

result

당신은 내가 해결해야 할 두 가지 일이있다 볼 수 있듯이 : 회전 된 사각형이 작동

  1. 가 결선 위치에 있지만. 4
  2. 이미지의 배경이 검은 색으로 변경되었습니다. 이전에는 투명 함을 나타 냈습니다.
+0

모두 하나의 이미지이거나 4 개의 별도 이미지입니까? – chornge

+0

이것이 모두 하나의 이미지라면, 그냥 그려 보는 것이 더 쉬울 것입니다. –

+0

@chornge 아니, 모두 하나의 이미지입니다. – Daniele

답변

2

회전하려는 영역의 좌표와 크기를 알고 있거나 이해할 수 있으면 비교적 간단합니다.

  1. 이미지를 변경할 수있는 Bitmap으로로드하십시오.
  2. 원본에서 원하는 영역의 두 번째 회전 된 Bitmap을 만듭니다.
  3. 원래 BitmapCanvas을 만듭니다.
  4. 필요한 경우 클리핑 된 영역을 지 웁니다.
  5. 회전 된 영역을 원본에 다시 그립니다. 다음 예에서

, 지역의 좌표 (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); 

편집의 문제를 해결하기 위해 : 일본어 예의

  1. 회전 된 영역의 숫자는 수직 장축과 화상에 기초 하였다. 수정 된 이미지는 해당 지역이 수정 된 이후에 수직 으로 회전되었습니다.

  2. 검은 색 배경은 결과 이미지를 MediaStore에 삽입 한 것이므로 투명도를 지원하지 않는 JPEG 형식으로 이미지를 저장합니다.

+1

제공하신 큰 도움에 다시 한 번 감사드립니다. – Daniele