Shape Drawable은 Drawable 객체를 XML로 생성하는 것이다.
예를 들면, 버튼 모양이나 배경 화면을 꾸밀 때 비트맵을 사용하지 않고 직접 XML로 만든다.
비트맵을 내부에 저장하는 것보다 XML로 만드는 편이 APK 의 용량을 줄일 수 있다.

파일은 res/drawable 안에 .xml 형태로 저장된다.
파일명은 대문자를 사용하지 않는다.

<!--syntax-->

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle","oval","line","ring"] >
    <corners
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient
        android:angle="integer"
        android:centerX="float"
        android:centerY="float"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear","radial","sweep"]
        android:useLevel=["true","false"] />
    <padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />
    <size
        android:width="integer"
        android:height="integer" />
    <solid
        android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />
</shape>


<shape>
도형의 형태를 정의하는 xml의 루트 요소.

xmlns:android=”string”
   XML의 namespace.
   반드시 “http://schemas.android.com/apk/res/android” 로 해야 한다.

android:shape=”keyword”
   shape의 형태를 지정한다.
   사용 가능한 값은 기본 shape인 “rectangle” 사각형, 타원 shape “oval” 원, “line” 선, “ring” 고리형 shape가 있다.
   android:shape=”line” 의 경우 반드시 요소를 사용해 선의 두께를 정의해야 한다.
   만일 android:shape="ring" 으로 정의할 경우 다음 속성을 사용할 수 있다.

android:innerRadius=”dimension”
   고리 안쪽 원의 반지름 값을 지정한다.

android:innerRadiusRatio=”float”
   고리의 전체 너비에 대한 안쪽 원의 반지름 비율 값으로, 기본값은 9다.
   값이 5일 경우 전체 너비를 5로 나눈 값이 안쪽 원의 반지름이 된다.
   android:innerRadius가 같이 정의되어 있다면 android:innerRadiusRatio 값은 적용되지 않는다.

android:thickness=”dimension”
   고리의 두께.

android:thicknessRatio=”float”
   고리 전체 너비에 대한 고리의 두께 비율 값으로, 기본값은 3이다.
   값이 2일 경우 전체 너비를 2로 나눈 값이 고리의 두께가 된다.
   android:innerRadius가 같이 정의되어 있다면 android:thicknessRatio 값은 적용되지 않는다.

android:useLevel=”boolean”
   일반적으로 “false”를 사용한다.
   LevelListDrawable로 사용할 경우 “true”를 쓰며, Shape가 보이지 않게 된다.

<corners>
모서리를 둥글게 한다. shape가 “rectangle”일 경우에만 적용된다.

android:radius=”dimension”
   모든 모서리의 반지름 값을 지정한다.
   모서리를 둥글게 하고 싶지 않다면 0(0dp) 값을 사용한다.
   다음 속성을 통해 각 모서리의 값을 정의하면 android:radius는 적용되지 않는다.

android:topLeftRadius=”dimension”
   좌측 상단 모서리의 반지름 값을 지정한다.

android:topRightRadius=”dimension”
   우측 상단 모서리의 반지름 값을 지정한다.

android:bottomLeftRadius=”dimension”
   좌측 하단 모서리의 반지름 값을 지정한다.

android:bottomRightRadius=”dimension”
   우측 하단 모서리의 반지름 값을 지정한다.

<gradient>
색상의 기울기를 정의한다.

android:angle=”integer”
   색상의 기울기 각도(단위:도)를 을 지정한다.
   값은 45의 배수여야 한다.
   기본값은 0으로, 왼쪽에서 오른쪽으로 진행된다.
   값 90은 아래에서 위로 진행된다.

android:centerX=”float”
   색상의 기울기 중심에 대한 X축 위치의 상대적인 값을 지정한다. (0~1.0)
   사각형을 예로 들면, 0에 가까운 값은 사각형의 왼쪽 면을 중심으로 하고 1에 가까운 값은 사각형의 오른쪽 면을 중심으로 한다.
   android:type=”linear”에는 적용되지 않는다.

android:centerY=”float”
   색상의 기울기 중심에 대한 Y축 위치의 상대적인 값을 지정한다. (0~1.0)
   사각형을 예로 들면, 0에 가까운 값은 사각형의 위쪽 면을 중심으로 하고 1에 가까운 값은 사각형의 아래쪽 면을 중심으로 한다.
   android:type=”linear”에는 적용되지 않는다.

android:centerColor=”color”
   시작 색상과 끝 색상 사이에 나타나는 중간 색상 값을 지정한다.
   16진수 값이나 색상 리소스로 지정된다.

android:endColor=”color”
   gradient 끝 지점의 색상 값을 지정한다.

android:gradientRadius=”float”
   gradient의 반지름 값을 지정한다.
   android:type=”radial”인 경우에만 적용된다.

android:startColor=”color”
   gradient 시작 지점의 색상 값을 지정한다.

android:type=”keyword”
   gradient의 패턴을 지정한다.
   사용 가능한 값으로는 기본값인 “linear” 선형, 시작 점이 가운데인 “radial” 방사형, sweeping line gradient “sweep”가 있다.

android:useLevel=”boolean”
   LevelListDrawable로 사용할 경우 “true”를 지정한다.

<padding>
shape를 포함하는 View에 적용할 패딩 값을 정의한다. shape에 지정되지 않는다.

android:left=”dimension”
   좌측 패딩 값을 지정한다.

android:top=”dimension”
   상단 패딩 값을 지정한다.

android:right=”dimension”
   우측 패딩 값을 지정한다.

android:bottom=”dimension”
   하단 패딩 값을 지정한다.

<size>
shape의 크기를 정의한다.
이는 곧 container view의 크기가 된다.
imageView에 사용할 경우 android:scaleType=”center”로 설정하여 제한할 수 있다.

android:height=”dimension”
   shape의 높이를 지정한다.

android:width=”dimension”
   shape의 너비를 지정한다.

<solid>
도형 안을 채울 단색을 정의한다.

android:color=”color”
   shape에 적용할 색상 값을 지정한다.
   16진수 값이나 색상 리소스로 지정한다.

<stroke>
도형의 테두리나 선을 그릴 때 정의한다.

android:width=”dimension”
   테두리 또는 선의 두께를 지정한다.

android:color=”color”
   테두리 또는 선의 색상을 지정한다.

android:dashGap=”dimension”
   점선의 간격을 지정한다.
   android:dashWidth가 설정된 경우에만 적용된다.

android:dashWidth=”dimension”
   점선의 크기를 지정한다.
   android:dashGap가 설정된 경우에만 적용된다.


참고 사이트