Android 使用Shape繪制圖形

Shape生成圖形,既簡單有實用,靈活性比較大,而且可以減少包的大?。?br> Android 使用Shape繪制背景圖片的步驟:
1.在drawable文件夾下新建一個xml文件
2.在xml中繪制圖形
3.在代碼中引用這個xml

官網(wǎng)Sample:

<?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="integer"
        android:centerY="integer"
        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>

1.Shape語法

shape:可以畫四種圖形,分別是:矩形(rectangle)、橢圓(oval)、線(line)、圓環(huán)(ring)。

android:shape=["rectangle" | "oval" | "line" | "ring"]

Solid: 填充顏色

<solid
        android:color="color" />

Corners: 圓角大?。?br> 四個角可以不同的弧度,也可以相同的弧度

<corners
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />

Size: 圖形的大小:

 <size
        android:width="integer"
        android:height="integer" />

Gradient: 關(guān)于漸變顏色
android:angle:漸變角度:0:左到右;90:下到上;180:右到左;270:上到下
android:centerX:表示漸變的X軸起始位置,范圍0-1,0.5表示圓心。
android:centerY:表示漸變的Y軸起始位置,范圍0-1,0.5表示圓心。
android:startColor:漸變起始顏色
android:endColor:漸變結(jié)束顏色
android:type:漸變類型,有三種

分別是:
linear 線性漸變,默認(rèn)的漸變類型
radial 放射漸變,設(shè)置該項時,android:gradientRadius也必須設(shè)置
sweep 掃描性漸變

<gradient
        android:angle="integer"
        android:centerX="integer"
        android:centerY="integer"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />

Stroke:邊框
android:width:邊框大小
android:color:邊框顏色

<stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />

Padding:設(shè)置內(nèi)邊距,4個方向的內(nèi)邊距

<padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />

示例:
1.圓形

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid android:color="@color/colorPrimary"></solid>

    <size android:height="100dp"
        android:width="100dp"></size>

</shape>

2.方形

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners android:radius="10dp"></corners>

    <solid android:color="@color/colorPrimary"></solid>

    <padding android:bottom="12dp"
        android:left="12dp"
        android:right="12dp"
        android:top="12dp"></padding>

</shape>

3.線型

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">

    <stroke
        android:width="2dp"        <!--填充顏色的高度-->
         android:color="@color/colorPrimaryDark"    <!--虛線間距寬度-->
         android:dashGap="3dp"    <!--虛線間距寬度-->
         android:dashWidth="4dp"></stroke>    <!--虛線寬度-->

    <!--線的高度,size大小必須大于stroke 的android:width-->
    <size android:height="3dp"></size>
</shape>

4.圓環(huán)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:useLevel="false"
    android:thickness="10dp">
  

    <solid android:color="@color/colorAccent"></solid>  

    <!--useLevel需要設(shè)置為false-->
    <!--android:thickness:圓環(huán)寬度-->
</shape>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,881評論 25 709
  • 徹夜未眠,我 起了個 大早 來到 學(xué)校,在 密集 的 人群中 尋找著 妳 的身影,等待 永遠(yuǎn)是 漫長...
    大王BA閱讀 162評論 0 1
  • 《遇見你,真好》 在白茫茫人海中相遇是我們的緣分,成為朋友是我們福分。遇見你,真好。在人生道路上有你真幸運,你給我...
    爽浪浪閱讀 104評論 0 0
  • 這周我覺得非常滿足,因為我把電影《霍比特人1:意外之旅》、《霍比特人2:史矛革之戰(zhàn)》、《霍比特人3:五軍之戰(zhàn)》都看...
    陌霄_Lu閱讀 3,282評論 1 3

友情鏈接更多精彩內(nèi)容