본문 바로가기

IT/안드로이드 관련

[안드로이드] ripple effect 적용하기

안녕하세요 남갯입니다.


오늘은 Ripple Effect라는 것에대해 포스팅 해보려고합니다.


Ripple Effect는 처음 안드로이드 5.0에서 소개되었습니다.





각자 버튼에 적용하는 default의 방법으로는 아래와같이 적용하고 난 후에


android:background="?android:attr/selectableItemBackground"


min sdk api가 21이 넘는다면 아래와같이 적용함으로써 가능합니다.

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:colorControlHighlight">@color/colorPrimaryDark</item>
</style>





21이상이 되면서 혹은 원하는형태로 custom을 하기위해서는


drawable에 ripple_custom xml을 만듭니다.



<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#008FFC">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#008FFC" />
<corners android:radius="2dip" />
</shape>
</item>

<item android:id="@android:id/background">
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="@android:color/white"
android:startColor="@android:color/white"
android:type="linear" />
<corners android:radius="2dip" />
</shape>
</item>
</ripple>


위와 같이 커스텀을해서 


위쪽에는 클릭을 적용후에 동작할 클릭을 적용하면 됩니다.

아래에는 클릭을 안했을대의 기본적인 색상을 적용하면 됩니다.



그후 버튼에 아래와 같이 적용하면 끝!

android:background="@drawable/ripple_custom"