이미지가 들어왔을 때 원하는 width, height로 비율에 맞게 잘라준다.
https://stackoverflow.com/questions/4837715/how-to-resize-a-bitmap-in-android
스택오버플로우에서 scale처리하는 함수 토대로 만들었다.
private fun getResizedBitmap(bm: Bitmap, destWidth: Int, destHeight: Int): Bitmap? {
val width = bm.width
val height = bm.height
if(width >= height)
{
// 세로 기준으로 비율을 잡아 확대하고 이미지 좌우를 자른다.
val heightRatio = (destHeight / height.toFloat())
val matrix = Matrix()
matrix.postScale(heightRatio, heightRatio)
val resizedBitmap = Bitmap.createBitmap(
bm, 0, 0, width, height, matrix, false
)
return Bitmap.createBitmap(
resizedBitmap,
(resizedBitmap.width - destWidth) / 2,
0,
destWidth,
destHeight
)
}
// 가로 기준으로 비율을 잡아 확대하고 이미지 위아래를 자른다.
val widthRatio = (destWidth / width.toFloat())
val matrix = Matrix()
matrix.postScale(widthRatio, widthRatio)
val resizedBitmap = Bitmap.createBitmap(
bm, 0, 0, width, height, matrix, false
)
return Bitmap.createBitmap(
resizedBitmap,
0,
(resizedBitmap.height - destHeight) / 2,
destWidth,
destHeight
)
}
'프로그래밍 > Android, iOS' 카테고리의 다른 글
안드로이드 BLE 통신(공식문서 번역) 1 (0) | 2023.07.23 |
---|---|
[Android] 사이닝 처리 하기 (0) | 2022.12.09 |
[Android] Network status (0) | 2022.05.01 |
[Android] EditText 주로 쓰는 옵션 (0) | 2022.04.01 |
[Google Play Store] 어플 키워드 관련 정리 (0) | 2022.03.31 |
댓글