반응형
안녕하세요!
오늘은 Bitmap을 이용해 둥근 모서리 이미지를 만드는 간단한 코드를 소개 시켜드리고자 합니다.
Glide와 같은 유용한 이미지 라이브러리를 이용해서도 구현 가능하지만,
이런 서드파티 라이브러리 없이 사용 해야할 경우 유용할 것 같습니다!
코드는 아래와 같습니다.
(+ 추가적으로 파일 경로를 이용해 ImageView에 이미지를 뿌려주는 코드도 같이 담겨 있습니다😉)
fun setPhoto(filePath: String) {
val file = File(filePath)
val srcBitmap = BitmapFactory.decodeFile(file.absolutePath)
srcBitmap?.let { bitmap ->
val roundedBitmap = getRoundedCornerBitmap(bitmap)
roundedBitmap?.let { rb -> ivPhoto.setImageBitmap(rb) }
}
}
private fun getRoundedCornerBitmap(bitmap: Bitmap): Bitmap? {
val output = Bitmap.createBitmap(
bitmap.width,
bitmap.height,
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(output)
val paint = Paint()
val rect = Rect(0, 0, bitmap.width, bitmap.height)
val rectF = RectF(rect)
val roundPx = 30f
canvas.drawARGB(0, 0, 0, 0)
canvas.drawRoundRect(rectF, roundPx, roundPx, paint)
paint.isAntiAlias = true
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
canvas.drawBitmap(bitmap, rect, rect, paint)
return output
}
참고
https://shary1012.tistory.com/305
android 비트맵 라운드 처리 (Rounded Bitmap)
안드로이드에서 이미지 작업을 쉽게 할 수 있도록 제공되는 서드파티 라이브러리가 많이 있지만, 때로는 서드파티 라이브러리를 사용할 수 없어서 직접 핸들링을 해야 하는 경우가 있다. private
shary1012.tistory.com
반응형
'Android' 카테고리의 다른 글
[안드로이드 / Kotlin] FusedLocationProviderClient (0) | 2023.05.25 |
---|---|
[안드로이드 / Kotlin] Coroutine Flow debounce를 이용한 검색 구현 (0) | 2023.03.16 |
[안드로이드 / Kotlin] RecyclerView 최상단 최하단 스크롤감지 (0) | 2022.11.11 |
[안드로이드 / Kotlin] Snackbar에 커스텀 폰트 적용하기 (0) | 2022.11.08 |
[안드로이드] ConstraintLayout 뷰 밀림 문제 (0) | 2022.10.30 |