Today I Learn

TIL - Android CircleImageView

SeungYong.Lee 2023. 1. 6. 13:51
반응형

안드로이드 UI 작업 중, 간혹 원 모양의 View가 필요한 경우가 있습니다.

 

다음과 같이 Background Resource를 커스텀으로 만들어주는 방법도 있으나,

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#ffffff" />
</shape>

 

de.hdodenhof:circleimageview 라이브러리를 통해 Circle Image View를 바로 생성하는 방법 또한 있습니다.

https://github.com/hdodenhof/CircleImageView

 

ImageView를 확장해서 만들어진 라이브러리이기 때문에 기본 사용법은 ImageView와 동일합니다.

implementation 'de.hdodenhof:circleimageview:3.1.0'

 

아래와 같이 레이아웃을 삽입해주고, 각종 속성들을 설정해줍니다.

옵션 중 civ_border_~ 형태의 옵션들을 통해 CircleView의 테두리들에 대한 추가 설정이 가능합니다.

<de.hdodenhof.circleimageview.CircleImageView
    android:id="@+id/contactImg"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:civ_border_color=""
    app:civ_border_width=""
    app:civ_border_overlay=""
    android:visibility="gone"/>

 

이후에는 이미지 라이브러리를 활용하여 로직에 따라 적용해주시면 됩니다.

if (photoUrl.isNotEmpty()) {
    Glide.with(context)
        .load(photoUrl)
        .into(holder.circleImg)
} else {
    Glide.with(context)
        .load(R.drawable.profile)
        .into(holder.circleImg)
}

 

반응형