Android中使用shape詳解
大家平時在做頁面的時候,經常會使用一些比較規則的圖形(比如:圓形,圓角矩形,橢圓,圓環······),這個時候通常有三種方法來解決。
一、如果公司裏面有專業的美工妹妹,你只需要請她們喝瓶飲料,她們可以幫你切出你想要的圖,問題就解決了;
二、java代碼寫,主要就是android中paint的用法,會在我後期的博客中寫到;
三、shaped 的使用,這個是最最好用的方法,也是開發android最常用的方法。
下面我主要圍繞shape來講解一下:
1.先了解一下shape中哪幾種類型(可以畫那些圖形)
line(劃線):可以是實線,也可以是虛線;
oval(橢圓):這裏面的橢圓是標準的橢圓,腳墊距離是系統控制的,不可以更改,若果想畫圓,直接將長寬設置成相同就可以了;
rectangle(矩形):這個是默認的類型,不多介紹
ring(環形):用的範圍太少,基本不適用,下面能用到再介紹
2.shape中設計的屬性
solid(實體):填充圖形裏面的顏色
<solid android:color="#ff0000"></solid>
gradient(漸變):跟實體顏色恰恰是兩個不同的概念,這個裏面控制的是漸變的顏色
android:startColor和android:endColor分別爲起始和結束顏色,ndroid:angle是漸變角度,必須爲45的整數倍。
stroke(畫筆):設置外圍邊框的顏色和寬度
<stroke android:color="#000000"
android:width="1dp"/>
corners(邊角圓弧):設置四個角的圓形程度
<corners
android:topRightRadius="20dp" 右上角
android:bottomLeftRadius="20dp" 右下角
android:topLeftRadius="1dp" 左上角
android:bottomRightRadius="0dp" 左下角
/>
padding(邊距):不詳細介紹
3.下面給大家舉個簡單的例子,給一個按鈕實現圓角的背景
效果圖
(1)、第一個按鈕,圓角,純色,紅色邊緣線
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00ff00"/>
<stroke android:color="#ff0000"
android:width="1dp"/>
<corners android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"
/>
<padding android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp"/>
</shape>
(2)、第二個按鈕,圓角,漸變,紅色邊緣線
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#00ff00"
android:endColor="#0000ff"
android:angle="45"></gradient>
<stroke android:color="#ff0000"
android:width="1dp"/>
<corners android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"
/>
<padding android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp"/>
</shape>
(3)、紅色圓形
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#ff0000"/>
</shape>
//下面還會更新博客