본문 바로가기
개발/Android

[Android] CardView의 그림자(음영) 없애기

by 감자감자곰 2023. 9. 30.

CardView란?

안드로이드에서는 CardView를 사용해 뷰의 모서리를 둥글게 처리하고, 그림자 효과를 간편하게 처리할 수 있다. 사실 그림자 효과가 미미하게 들어가 있어 이를 제거해야겠다는 생각을 미처 하지 못했는데, 코드 리뷰를 통해 다른 분이 이 부분을 알려주셔서 CardView에서 그림자를 제거하는 방법을 알아보고자 한다.


1. 기존 코드

<androidx.cardview.widget.CardView
    android:id="@+id/stuffA_img_profile"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_margin="15dp"
    app:cardCornerRadius="100dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@id/stuffA_ll_profile_address"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@drawable/img_dog" />

</androidx.cardview.widget.CardView>

CardView는 Android Jetpack 라이브러리의 일부이기에, android:elevation 속성으로는 CardView의 음영을 관리해줄 수 없다. 따라서, app:cardElevation 속성을 사용해 CardView의 음영을 조절해 줄 수 있다.


2. 수정한 최종 코드

<androidx.cardview.widget.CardView
    android:id="@+id/stuffA_img_profile"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_margin="15dp"
    app:cardCornerRadius="100dp"
    app:cardElevation="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@id/stuffA_ll_profile_address"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@drawable/img_dog" />

</androidx.cardview.widget.CardView>

초기 코드에서 app:cardElevation="0dp"만 추가해 CardView의 음영을 없앤 최종 코드이다.


결과 영상

app:cardElevation="0dp"라는 줄의 추가 여부에 따라 오른쪽에 보이는 사진의 음영 여부가 달라지는 것을 볼 수 있다. 미묘한 부분이지만, 추후 디자이너의 요청 사항에 알맞게 코딩하려면 이런 부분도 꼼꼼하게 짚고 넘어가야함을 배울 수 있었다 :)

댓글