[안드로이드] 버튼 상태 drawable

2021. 4. 26. 18:40앱개발

*용도

 1) 버튼 : Press & Release 시각 효과를 위해 쓰임

 

* 사용 방법

 1) 버튼의 이미지, 버튼 눌러졌을 때 이미지를 app/res/drawable에 복사한다

 

 

 

 2) 좌측 메뉴 트리 : app > res > drawable 에서 마우스 우클릭 > New > Drawable resource file

3) 사용할 xml 파일명을 입력한 다음 OK. (여기서는 button_drawable.xml)

4) xml의 내용을 다음과 같이 수정한다

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/button_press" />
    <item android:drawable="@drawable/button" />
</selector>

 

5) activity에 버튼을 추가한 다음 background 속성에 @drawable/button_drawable 입력

* 시뮬레이터로 실행해 보면 다음과 같이 확인 가능하다

 

* Drawable

 - Bitmap Drawable : 이미지를 메모리로 올렸을 때 안드로이드에서 정의한 타입이 비트맵. 이미지를 이용해서 그려줄 수 있는 것이 Drawable

 - 상태 Drawable : 상태에 따라서 다른 비트맵, 그래픽 또는 이미지를 보여줄 수 있는 것

 - trainsition Drawable : 두개의 drawable을 서로 바꿔줄 수 있는 것

  - shape drawable : 사각형, 삼각형을 이용해서 그려줄 수 있는 것