понедельник, 20 февраля 2012 г.

Android XML Drawables

Файлы храним в /res/drawable
Внутри кнопки добавляем 
android:background="@drawable/my_button_selector"















my_button_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/my_button_normal"></item>
    <item android:drawable="@drawable/my_button_focused" android:state_focused="true"></item>
    <item android:drawable="@drawable/my_button_pressed" android:state_pressed="true"></item>
</selector>
my_button_pressed.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <corners android:radius="12px" />
    <gradient
        android:angle="270"
        android:endColor="#79C83F"
        android:startColor="#7FA40E"
        android:type="linear" />
    <padding
        android:bottom="7px"
        android:left="4px"
        android:right="4px"
        android:top="7px" />
    <stroke
        android:width="2px"
        android:color="#52B10C" />
</shape>
my_button_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <corners android:radius="12px" />
    <gradient
        android:angle="90"
        android:endColor="#B7EF8E"
        android:startColor="#7FA40E"
        android:type="linear" />
    <padding
        android:bottom="7px"
        android:left="4px"
        android:right="4px"
        android:top="7px" />
</shape>
my_button_focused.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <corners android:radius="12px" />
    <gradient
        android:angle="90"
        android:endColor="#79C83F"
        android:startColor="#7FA40E"
        android:type="linear" />
    <padding
        android:bottom="7px"
        android:left="4px"
        android:right="4px"
        android:top="7px" />
</shape>
my_button2.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <corners android:radius="3px" />
    <gradient
        android:angle  ="90"
        android:gradientRadius ="360"
        android:startColor ="#F00"
        android:endColor ="#0F0"
        android:type  ="radial" />
    <padding
        android:bottom ="10px"
        android:left ="10px"
        android:right ="10px"
        android:top ="10px" />
</shape>
my_button3.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <corners android:radius="3px" />
    <gradient
        android:angle  ="90"
        android:startColor ="#F00"
        android:centerColor ="#00F"
        android:endColor ="#0F0"
        android:type  ="sweep" />
    <padding
        android:bottom ="10px"
        android:left ="10px"
        android:right ="10px"
        android:top  ="10px" />
</shape>
my_button4.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >
    <corners android:radius="3px" />
    <gradient
        android:angle="90"
        android:endColor="#0F0"
        android:gradientRadius="360"
        android:startColor="#F00"
        android:type="radial" />
    <stroke
        android:width="2dp"
        android:color="#FFF" />
</shape>
my_button5.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line" >
    <corners android:radius="12px" />
    <gradient
        android:angle="90"
        android:endColor="#B7EF8E"
        android:startColor="#7FA40E"
        android:type="linear" />
    <stroke
        android:width="1dp"
        android:color="#0F0" />
    <size
        android:height ="20dp"
        android:width ="20dp" />
    <solid android:color="#0F0" />
    <padding
        android:bottom ="50px"
        android:left ="50px"
        android:right ="50px"
        android:top ="50px" />
</shape>
my_button6.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadiusRatio="4"
    android:shape="ring"
    android:thickness="4dp"
    android:useLevel="false" >
    <solid android:color="#FF0000" />
    <size
        android:height="10dp"
        android:width="10dp" />
</shape>
my_button7.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <corners android:radius="4px" />
    <solid android:color="#030" />
    <stroke
        android:width="2dp"
        android:color="#0E0" />
</shape>
my_button8.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <corners android:radius="4px" />
    <solid android:color="#0A0" />
    <stroke
        android:width="2dp"
        android:color="#050" />
</shape>