Android利用shape實(shí)現(xiàn)各種簡(jiǎn)單的形狀
一,概述
我們?cè)赼ndroid開發(fā)中經(jīng)常要用到圖片,而一些簡(jiǎn)單的圖片我們完全可以用shape形狀drawable資源代替,使用shape有一個(gè)好處就是可以減小我們apk的大小,因?yàn)橥瑯拥男Ч?,shape比圖片更節(jié)省空間,好了,我們廢話不多說,下面進(jìn)入正題。
二,shape初識(shí)
shape是android drawable資源中的一個(gè)重要的角色,drawable資源覆蓋面廣,它不僅代表圖片,它可以是一個(gè)顏色,一個(gè)形狀,因?yàn)閟hape其簡(jiǎn)單實(shí)用,下面我們來看一下shape形狀的分類:
ectangle:
rectangle代表者矩形,它是shape默認(rèn)的形狀類型,即如果我們不在shape的android:shape屬性指定其類型時(shí),默認(rèn)是矩形,用它我們可以畫一個(gè)矩形,圓角矩形,具體在下面會(huì)說道
oval:
ovel,橢圓,用它可以畫橢圓,圓
line:
水平線,在使用該形狀的時(shí)候,我們得給它指定stroke元素指定其寬度,不然在使用該形狀的時(shí)候會(huì)報(bào)空指針異常
ring:
環(huán)形
下面我們來用上面說道的各種形狀畫圖形,打造各種簡(jiǎn)單的形狀
三,shape的使用
下面看看用shape畫的一些簡(jiǎn)單的圖形,之后我會(huì)按照?qǐng)D形說一下shape的各種屬性以及一些要注意的問題:
1.實(shí)心長(zhǎng)方形
<shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 設(shè)置固定填充色 --> <solid android:color="#f00" /> <size android:width="60dp" android:height="30dp"/> </shape>
2.炫彩實(shí)心長(zhǎng)方形
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <size android:width="60dp" android:height="30dp"/> <!-- 設(shè)置漸變填充色 --> <gradient android:startColor="#00f" android:centerColor="#0f0" android:endColor="#f00"></gradient> </shape>
3.長(zhǎng)方形外框
<shape xmlns:android="http://schemas.android.com/apk/res/android" > <size android:width="60dp" android:height="30dp"/> <!-- 設(shè)置描邊 --> <stroke android:width="2dp" android:color="#f00" ></stroke> </shape>
4.虛線長(zhǎng)方形外框
<shape xmlns:android="http://schemas.android.com/apk/res/android" > <size android:width="60dp" android:height="30dp"/> <!-- 設(shè)置描邊 --> <stroke android:width="2dp" android:color="#f00" android:dashWidth="5dp" android:dashGap="5dp"></stroke> </shape>
5.橢圓虛線外框
<shape xmlns:android="http://schemas.android.com/apk/res/android" > <size android:width="60dp" android:height="30dp"/> <!-- 設(shè)置描邊 --> <stroke android:width="2dp" android:color="#f00" android:dashWidth="5dp" android:dashGap="5dp"></stroke> <corners android:radius="15dp"/> </shape>
6.實(shí)心長(zhǎng)方體切圓角
<shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 設(shè)置固定填充色 --> <solid android:color="#f00" /> <size android:width="60dp" android:height="30dp"/> <corners android:radius="10dp"/> </shape>
7.
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <size android:width="60dp" android:height="30dp"/> <!-- 設(shè)置漸變填充色 --> <gradient android:startColor="#f00" android:centerColor="#0f0" android:endColor="#00f" android:gradientRadius="60" android:type="radial"></gradient> </shape>
8.
<shape xmlns:android="http://schemas.android.com/apk/res/android" > <size android:width="60dp" android:height="30dp"/> <!-- 設(shè)置描邊 --> <stroke android:width="2dp" android:color="#f00" ></stroke> <corners android:radius="15dp"/> </shape>
9.
<shape xmlns:android="http://schemas.android.com/apk/res/android" > <size android:width="60dp" android:height="30dp"/> <!-- 設(shè)置漸變填充色 --> <gradient android:startColor="#f00" android:centerColor="#0f0" android:endColor="#00f" android:type="sweep"></gradient> </shape>
10.
<shape xmlns:android="http://schemas.android.com/apk/res/android" > <size android:width="60dp" android:height="30dp"/> <!-- 設(shè)置描邊 --> <stroke android:width="2dp" android:color="#f00" android:dashWidth="5dp" android:dashGap="5dp"></stroke> <corners android:radius="15dp" android:topRightRadius="0dp" android:bottomRightRadius="0dp"/> </shape>
11.
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <!-- 設(shè)置固定填充色 --> <solid android:color="#f00" /> <size android:width="60dp" android:height="30dp"/> </shape>
12.
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:width="60dp" android:height="30dp"/> <!-- 設(shè)置漸變色 --> <gradient android:startColor="#00f" android:centerColor="#0f0" android:endColor="#f00"></gradient> </shape>
13.
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:width="60dp" android:height="30dp"/> <!-- 設(shè)置描邊色 --> <stroke android:width="2dp" android:color="#f00" ></stroke> </shape>
14.
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:width="60dp" android:height="30dp"/> <!-- 設(shè)置漸變填充色 --> <gradient android:startColor="#f00" android:centerColor="#0f0" android:endColor="#00f" android:gradientRadius="60" android:type="sweep"></gradient> </shape>
15.
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:width="60dp" android:height="30dp"/> <!-- 設(shè)置描邊色 --> <stroke android:width="2dp" android:color="#f00" android:dashWidth="5dp" android:dashGap="5dp"></stroke> </shape>
16.
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:innerRadius="20dp" android:shape="ring" android:thickness="2dp" android:useLevel="false" > <!-- 設(shè)置固定填充色 --> <solid android:color="#f00" /> <size android:height="44dp" /> </shape>
17.
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:innerRadius="20dp" android:shape="ring" android:thickness="2dp" android:useLevel="false" > <!-- 設(shè)置漸變填充色 --> <gradient android:startColor="#00f" android:centerColor="#0f0" android:endColor="#f00"/> <size android:height="44dp" /> </shape>
18.
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:innerRadius="20dp" android:shape="ring" android:thickness="2dp" android:useLevel="false" > <!-- 設(shè)置漸變填充色 --> <gradient android:startColor="#00f" android:centerColor="#0f0" android:endColor="#f00"/> <size android:height="44dp" /> <stroke android:width="2dp" android:color="#f00" android:dashWidth="5dp" android:dashGap="5dp"/> </shape>
19.
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:innerRadius="20dp" android:shape="ring" android:thickness="2dp" android:useLevel="false" > <size android:height="44dp" /> <stroke android:width="2dp" android:color="#f00" android:dashWidth="5dp" android:dashGap="5dp"/> </shape>
20.
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:innerRadius="20dp" android:shape="ring" android:innerRadiusRatio="8" android:useLevel="false" > <!-- 設(shè)置漸變填充色 --> <gradient android:startColor="#00f" android:centerColor="#0f0" android:endColor="#f00"/> <size android:height="44dp" /> </shape>
21.
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line"> <stroke android:width="1dp" android:color="#f00"/> <size android:height="2dp"></size> </shape>
22.
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line"> <stroke android:width="1dp" android:color="#f00"/> <size android:height="10dp"></size> </shape>
23.
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line" > <stroke android:dashGap="8px" android:dashWidth="8px" android:width="1dp" android:color="#f00" /> <size android:height="30dip" /> </shape>
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- Android編程使用自定義shape實(shí)現(xiàn)shadow陰影效果的方法
- Android自定義shape的使用示例
- Android控件系列之Shape使用方法
- Android中的Shape和Selector的結(jié)合使用實(shí)例
- Android中shape定義控件的使用
- Android shape和selector 結(jié)合使用實(shí)例代碼
- Android開發(fā)教程之shape和selector的結(jié)合使用
- Android中drawable使用Shape資源
- 詳解android shape的使用總結(jié)
- Android使用Shape實(shí)現(xiàn)ProgressBar樣式實(shí)例
相關(guān)文章
Android自定義PopupWindow仿點(diǎn)擊彈出分享功能
這篇文章主要為大家詳細(xì)介紹了Android自定義PopupWindow仿點(diǎn)擊彈出分享功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02基于DownloadManager的簡(jiǎn)單下載器編寫小結(jié)
Android自帶的DownloadManager是一個(gè)很好的下載文件的工具。該類在API level 9之后出現(xiàn),它已經(jīng)幫我們處理了下載失敗、重新下載等功能,整個(gè)下載過程全部交給系統(tǒng)負(fù)責(zé),不需要我們過多的處理,非常的nice。關(guān)鍵的是用起來也很簡(jiǎn)單,稍微封裝一下就可以幾句話搞定下載2017-12-12Android學(xué)習(xí)教程之下拉刷新實(shí)現(xiàn)代碼(11)
這篇文章主要為大家詳細(xì)介紹了Android學(xué)習(xí)教程之下拉刷新實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11使用Retrofit下載文件并實(shí)現(xiàn)進(jìn)度監(jiān)聽的示例
這篇文章主要介紹了使用Retrofit下載文件并實(shí)現(xiàn)進(jìn)度監(jiān)聽的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08Android性能測(cè)試關(guān)注的指標(biāo)整理
在本篇文章里小編給各位整理的是關(guān)于Android性能測(cè)試關(guān)注的指標(biāo)整理內(nèi)容,有興趣的朋友們學(xué)習(xí)下。2019-10-10Android 6.0動(dòng)態(tài)權(quán)限申請(qǐng)教程
本文主要介紹了Android 6.0動(dòng)態(tài)權(quán)限申請(qǐng)的教程,具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03Android自定義View實(shí)現(xiàn)數(shù)獨(dú)游戲
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)數(shù)獨(dú)游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android編程實(shí)現(xiàn)webview執(zhí)行l(wèi)oadUrl時(shí)隱藏鍵盤的workround效果
這篇文章主要介紹了Android編程實(shí)現(xiàn)webview執(zhí)行l(wèi)oadUrl時(shí)隱藏鍵盤的workround效果,較為詳細(xì)的分析了執(zhí)行l(wèi)oadUrl時(shí)隱藏鍵盤的workround具體步驟與兩種實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10C/C++在Java、Android和Objective-C三大平臺(tái)下實(shí)現(xiàn)混合編程
本文主要介紹C/C++在Java、Android和Objective-C三大平臺(tái)下實(shí)現(xiàn)混合編程,這里舉例說明實(shí)現(xiàn)不同平臺(tái)用C/C++實(shí)現(xiàn)編程的方法,有興趣的小伙伴可以參考下2016-08-08