정적 이미지를 앱에 표시해야 할 때 Drawable 클래스를 사용하여 도형과 이미지를 그릴 수 있다. XML을 이용하여 그래픽을 그린 후 /app/res/drawable
폴더에 저장하여 이미지처럼 사용할 수 있다.
상태 드로어블은 뷰의 상태에 따라 다른 그래픽을 참조할 수 있다.
/app/res/drawable
폴더에 새로운 Drawble resource file을 생성 후 다음과 같이 코드를 작성하면 된다.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@drawable/picture_pressed"
/>
<item android:drawable="@drawable/picture"
</selector>
state_pressed
속성은 눌린 상태를 뜻한다. 이렇게 코드를 작성한 후, 버튼의 background
속성값을 @drawable/xml파일명
으로 해주면 된다.
그렇게 되면 버튼을 눌렀을때 버튼의 배경은 picture_pressed.png가 되고, 누르지 않았을 때는 picture.png 가 된다.
셰이프 드로어블은 XML로 도형을 그릴 수 있다. app/res/drawble
폴더에 xml파일을 추가한 후 코드를 작성하면 된다.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:width="100dp" android:height="100dp" />
<stroke android:width="1dp" android:color="#000000"/>
<solid android:color="#00FFFF"/>
</selector>
최상위 태그를 <shape>
로 설정하고, android:shape
속성값을 rectangle로 설정해주면 사각형을 그릴수 있다.
<size>
: 도형의 크기
<stroke>
: 도형의 테두리
<solid>
: 도형의 안쪽