Android中Shape的用法詳解
ShapeDrawable是一種很常見的Drawable,可以理解為通過顏色來構(gòu)造的圖形,它既可以是純色的圖形,也可以是具有漸變效果的圖形,ShapeDrawabled語法稍顯復(fù)雜,如下所示:
<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>
•Android: shape
•有4個(gè)選項(xiàng),rectangle(矩形)oval(橢圓)line(橫線)ring(圓環(huán)),默認(rèn)為rectangle,需要注意line和ring需要通過標(biāo)簽來指定線的寬度和顏色等信息,否則無法達(dá)到預(yù)期效果
•首先來說一下最常用的rectangle(矩形),一般都是在按鈕或者字體上面設(shè)置一個(gè)background的Drawable。一般設(shè)置效果為正方形或者兩邊有弧度的形狀。
◦第一種情況就是設(shè)置矩形背景
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <size android:width="200dp" android:height="20dp" /> <solid android:color="#d22121"/> </shape>
通過設(shè)置size設(shè)置矩形的寬度和高度,*這里需要說明一下,咱們?cè)谶@里設(shè)置size的寬高,在最終顯示尺寸是沒有用的,也就是說當(dāng)你在一個(gè)控件中設(shè)置background的時(shí)候,這個(gè)shape是會(huì)被拉伸或者縮小為view的大小。*solid屬性設(shè)置矩形里面的背景顏色。
將背景色設(shè)置為漸變
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <size android:width="200dp" android:height="20dp" /> <gradient android:startColor="#fff" android:centerColor="#f1a9a9" android:endColor="#ec5b5b" android:type="linear" /> </shape>
效果圖:
這里默認(rèn)的type就是linear,里面還有其他兩個(gè)屬性可以選擇分別是radial(徑向漸變)和sweep(掃描漸變)
一般最常用的也就是線性漸變還有其他幾個(gè)屬性沒有用但是很好理解
android:angle——漸變的角度,默認(rèn)為0,其值必須是45的倍數(shù),0表示從左到右,90表示從下到上。
android:centerX——漸變的中心點(diǎn)橫坐標(biāo)
android:centerY——漸變的中心點(diǎn)縱坐標(biāo)
android:gradientRadiu——漸變半徑,僅當(dāng)android:type=”radial”時(shí)有效
•接下來說一下畫圓角的矩形背景
◦其實(shí)只用設(shè)置一下corners的屬性就是了。
◦具體詳細(xì)的說明
◦android:radius—— 給四個(gè)角設(shè)置相同的角度,優(yōu)先級(jí)較低,會(huì)被其他四個(gè)屬性覆蓋
android:bottomLeftRadius——設(shè)定左下角的角度
android:bottomRightRadius——設(shè)定右下角的角度
android:TopLeftRadius——設(shè)定左上角的角度
android:TopRightRadius——設(shè)定右上角的角度
接下來就是如何畫一個(gè)空心的背景
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <size android:width="200dp" android:height="20dp" /> <stroke android:width="1px" android:color="#ffff1c77" /> <corners android:radius="50dp"/> </shape>
效果圖如下
當(dāng)然里面也可以自由發(fā)揮設(shè)置漸變色,但是一般里面都純色。
◦這里里面也可以設(shè)置為虛線
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <size android:width="200dp" android:height="20dp" /> <stroke android:dashWidth="4dp" android:dashGap="2dp" android:width="1px" android:color="#ffff1c77" /> <corners android:radius="50dp"/> </shape>
好了,其實(shí)里面的東西很簡單,總結(jié)一下就好了。希望大家用的開心。
相關(guān)文章
Android控件Tween動(dòng)畫(補(bǔ)間動(dòng)畫)實(shí)現(xiàn)方法示例
這篇文章主要介紹了Android控件Tween動(dòng)畫(補(bǔ)間動(dòng)畫)實(shí)現(xiàn)方法,結(jié)合具體實(shí)例形式分析了Android補(bǔ)間動(dòng)畫的原理、功能實(shí)現(xiàn)與布局相關(guān)操作技巧,需要的朋友可以參考下2017-08-08Android利用懸浮按鈕實(shí)現(xiàn)翻頁效果
這篇文章主要介紹了Android利用懸浮按鈕實(shí)現(xiàn)翻頁效果的相關(guān)資料,需要的朋友可以參考下2015-12-12Android視頻播放器屏幕左側(cè)邊隨手指上下滑動(dòng)亮度調(diào)節(jié)功能的原理實(shí)現(xiàn)
這篇文章主要介紹了Android視頻播放器屏幕左側(cè)邊隨手指上下滑動(dòng)亮度調(diào)節(jié)功能的原理實(shí)現(xiàn),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02詳解Android中接口回調(diào)、方法回調(diào)
在Android開發(fā)中我們很多地方都用到了方法的回調(diào),回調(diào)就是把方法的定義和功能導(dǎo)入實(shí)現(xiàn)分開的一種機(jī)制,目的是為了解耦他的本質(zhì)是基于觀察者設(shè)計(jì)模式,即觀察者設(shè)計(jì)模式的的簡化版。本文主要對(duì)Android中接口回調(diào)、方法回調(diào)進(jìn)行詳細(xì)介紹,下面跟著小編一起來看下吧2017-01-01Android開發(fā)自學(xué)筆記(一):Hello,world!
這篇文章主要介紹了Android開發(fā)自學(xué)筆記(一):Hello,world!本文講解了創(chuàng)建HelloWorld工程、編寫代碼、啟動(dòng)模擬器等步驟,需要的朋友可以參考下2015-04-04Android原生ViewPager控件實(shí)現(xiàn)卡片翻動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android原生ViewPager控件實(shí)現(xiàn)卡片翻動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07