Android:使用shape製作素材

yufan發表於2016-01-08

最近看到朋友製作的Android APP使用了極少的圖片,但是圖形卻極其豐富,問了之後得知是使用shape繪製的,有很多優點。 
下面是我整理的一些素材:

預覽

下面是圖片預覽:

這裡寫圖片描述

程式碼

佈局檔案

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:background="@drawable/s1"
        android:padding="5dp"
        android:text="@string/s1"
        android:textColor="#fff"
        android:textSize="16sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:background="@drawable/s2"
        android:padding="5dp"
        android:text="@string/s2"
        android:textColor="#ff9800"
        android:textSize="16sp" />

    <ImageButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginBottom="20dp"
        android:background="@drawable/s3"
        android:padding="10dp"
        android:scaleType="fitXY"
        android:src="@drawable/lsearch" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:background="@drawable/s4"
        android:padding="5dp"
        android:text="@string/s4"
        android:textColor="#fff"
        android:textSize="16sp" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:background="@drawable/s5"
        android:padding="5dp"
        android:text="@string/s5"
        android:textColor="#00bcd4"
        android:textSize="16sp" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:background="@drawable/s6"
        android:padding="5dp"
        android:text="@string/s6"
        android:textColor="#fff"
        android:textSize="16sp" />

</LinearLayout>

shape檔案

綠色標籤s1.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#009688" />
    <corners android:radius="8dp" />
</shape>

 

橙色標籤s2.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#00000000" />
    <corners android:radius="8dp" />
    <stroke android:width="1dp" android:color="#ff9800" />
</shape>

 

藍色圓形按鈕s3.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <shape android:shape="oval">
            <solid android:color="#aa00bcd4" />
        </shape>
    </item>

    <item android:state_focused="true">
        <shape android:shape="oval">
            <solid android:color="#aa00bcd4" />
        </shape>
    </item>

    <item>
        <shape android:shape="oval">
            <solid android:color="#00bcd4" />
        </shape>
    </item>

</selector>

藍色按鈕s4.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <shape>
            <solid android:color="#aa00bcd4" />
            <corners android:radius="8dp" />
        </shape>
    </item>

    <item android:state_focused="true">
        <shape>
            <solid android:color="#aa00bcd4" />
            <corners android:radius="8dp" />
        </shape>
    </item>

    <item>
        <shape>
            <solid android:color="#00bcd4" />
            <corners android:radius="8dp" />
        </shape>
    </item>

</selector>

 

藍色邊框按鈕s5.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <shape>
            <solid android:color="#e3e3e3" />
            <corners android:radius="8dp" />
            <stroke android:width="1dp" android:color="#00bcd4" />
        </shape>
    </item>

    <item android:state_focused="true">
        <shape>
            <solid android:color="#e3e3e3" />
            <corners android:radius="8dp" />
            <stroke android:width="1dp" android:color="#00bcd4" />
        </shape>
    </item>

    <item>
        <shape>
            <solid android:color="#00000000" />
            <corners android:radius="8dp" />
            <stroke android:width="1dp" android:color="#00bcd4" />
        </shape>
    </item>

</select>

藍色帶陰影按鈕s6.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <layer-list>
            <item android:top="3dp">
                <shape>
                    <solid android:color="#00bcd4" />
                    <corners android:radius="8dp" />
                </shape>
            </item>
        </layer-list>
    </item>

    <item android:state_focused="true">
        <layer-list>
            <item android:top="3dp">
                <shape>
                    <solid android:color="#00bcd4" />
                    <corners android:radius="8dp" />
                </shape>
            </item>
        </layer-list>
    </item>

    <item>
        <layer-list>
            <item>
                <shape>
                    <solid android:color="#dddddd" />
                    <corners android:radius="8dp"/>
                </shape>
            </item>
            <item android:bottom="3dp">
                <shape>
                    <solid android:color="#00bcd4" />
                    <corners android:radius="8dp"/>
                </shape>
            </item>
        </layer-list>
    </item>

</selector>

相關文章