[안드로이드 / Kotlin] 둥근 모서리 Bitmap 이미지 만들기

2022. 11. 18. 10:19·Android
반응형

안녕하세요!

오늘은 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
'Android' 카테고리의 다른 글
  • [안드로이드 / Kotlin] FusedLocationProviderClient
  • [안드로이드 / Kotlin] Coroutine Flow debounce를 이용한 검색 구현
  • [안드로이드 / Kotlin] RecyclerView 최상단 최하단 스크롤감지
  • [안드로이드 / Kotlin] Snackbar에 커스텀 폰트 적용하기
서주냥
서주냥
안드로이드 개발 관련 끄적임
  • 서주냥
    AOS가 아니라 안드로이드
    서주냥
  • 전체
    오늘
    어제
    • 전체보기 (59)
      • 알고리즘 (12)
        • 백준 (4)
        • 프로그래머스 (5)
        • 개념 (3)
      • Android (44)
        • Compose (2)
      • Java (2)
      • Kotlin (1)
  • 링크

    • GitHub
  • 인기 글

  • 태그

    벨만 포드
    Hilt
    reified
    자바
    백준
    Clean Architecture
    다이나믹 프로그래밍
    RecyclerView
    lineHeight
    코틀린
    FusedLocationProviderClient
    SnackBar
    textstyle
    Coroutine
    최단 경로
    뷰모델
    투 포인터
    textunit
    프로그래머스
    안드로이드
    Coroutine Flow
    알고리즘
    BLE
    클린 아키텍처
    ConstraintLayout
    코루틴
    debounce
    컴포즈
    블루투스
    viewpager2
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
서주냥
[안드로이드 / Kotlin] 둥근 모서리 Bitmap 이미지 만들기
상단으로

티스토리툴바