Development/Android

[Android] XML 기반 Ripple 효과 구현

SeungYong.Lee 2025. 6. 20. 14:43
반응형

- XML 파일을 기반으로 Ripple을 구현하는 과정

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:attr/colorControlHighlight">
    <item android:id="@android:id/mask">
        <shape android:shape="rectangle">
            <corners android:radius="8dp" />
            <solid android:color="@color/color" />
        </shape>
    </item>
</ripple>

- drawable에 ripple 태그를 활용해 효과 시에 보일 모양을 지정할 수 있다.

 

- 그리고 생성한 리소스를 foreground로 설정해주면 완료

val tab = FrameLayout(context).apply {
    layoutParams = LinearLayout.LayoutParams(0, MATCH_PARENT, 1f).apply {
        setMargins(tabMargin / 2, tabMargin, tabMargin / 2, tabMargin)
    }
    isClickable = true
    isFocusable = true
    setBackgroundColor(Color.TRANSPARENT)
    elevation = 0f
    foreground = ContextCompat.getDrawable(context, R.drawable.tab_selected_background)
}

 

반응형