Android實(shí)現(xiàn) Shape屬性gradient 漸變效果
1,gradient(漸變)
【1】<gradient>用以定義漸變色,可以定義兩色漸變和三色漸變,及漸變樣式;
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" > <gradient android:type=["linear" | "radial" | "sweep"] //共有3中漸變類(lèi)型,線性漸變(默認(rèn))/放射漸變/掃描式漸變 android:angle="integer" //漸變角度,必須為45的倍數(shù),0為從左到右,90為從上到下 android:centerX="float" //漸變中心X的相當(dāng)位置,范圍為0~1 android:centerY="float" //漸變中心Y的相當(dāng)位置,范圍為0~1 android:startColor="color" //漸變開(kāi)始點(diǎn)的顏色 android:centerColor="color" //漸變中間點(diǎn)的顏色,在開(kāi)始與結(jié)束點(diǎn)之間 android:endColor="color" //漸變結(jié)束點(diǎn)的顏色 android:gradientRadius="float" //漸變的半徑,只有當(dāng)漸變類(lèi)型為radial時(shí)才能使用 android:useLevel=["true" | "false"] /> //使用LevelListDrawable時(shí)就要設(shè)置為true。設(shè)為false時(shí)才有漸變效果
首先有三種漸變類(lèi)型,分別是:linear(線性漸變)、radial(放射性漸變)、sweep(掃描式漸變)
</shape>
- android:useLevel屬性通常不使用。該屬性用于指定是否將該shape當(dāng)成一個(gè)LevelListDrawable來(lái)使用,默認(rèn)值為false。
- angle屬性確實(shí)只對(duì)線性漸變有效,其它兩種漸變方式都沒(méi)有任何動(dòng)靜
- centerX、centerY兩個(gè)屬性用于設(shè)置漸變的中心點(diǎn)位置,僅當(dāng)漸變類(lèi)型為放射漸變時(shí)有效。
2,Demo實(shí)現(xiàn)效果
- 我們使用三色漸變來(lái)看看這三種漸變方式都是怎么顯示的:(如果不使用centerColor屬性就是雙色漸變,這個(gè)屬性是可選的)
- 注意: 在構(gòu)造放射性漸變時(shí),要加上android:gradientRadius屬性(漸變半徑),即必須指定漸變半徑的大小才會(huì)起作用
【1】 線性漸變
實(shí)現(xiàn)效果

shape 代碼
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:type="linear"
android:startColor="#ff0000"
android:centerColor="#00ff00"
android:endColor="#0000ff"/>
</shape>
【2】 放射性漸變
實(shí)現(xiàn)效果

實(shí)現(xiàn)代碼
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:type="radial"
android:startColor="#ff0000"
android:centerColor="#00ff00"
android:endColor="#0000ff"
android:gradientRadius="100"/>
</shape>
【3】 掃描式漸變
實(shí)現(xiàn)效果

實(shí)現(xiàn)代碼
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:type="sweep"
android:startColor="#ff0000"
android:centerColor="#00ff00"
android:endColor="#0000ff"/>
</shape>
【4】android:angle屬性修改漸變角度 (僅對(duì)線性漸變有效)
1)android:angle="integer" //漸變角度,必須為45的倍數(shù),0為從左到右,90為從上到下
2)angle屬性確實(shí)只對(duì)線性漸變有效,其它兩種漸變方式都沒(méi)有任何動(dòng)靜
實(shí)現(xiàn)效果:
實(shí)現(xiàn)代碼:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:type="linear"
android:startColor="#ff0000"
android:centerColor="#00ff00"
android:endColor="#0000ff"
android:angle="45"/>
</shape>
【5】android:centerX與android:centerY
1) android:centerX="0.2",android:centerY="0.8"
2)centerX、centerY兩個(gè)屬性用于設(shè)置漸變的中心點(diǎn)位置,僅當(dāng)漸變類(lèi)型為放射漸變時(shí)有效。
3)類(lèi)型為分?jǐn)?shù)或小數(shù),不接受Dimension。默認(rèn)值是0.5,有效值是0.0~1.0,超出該范圍后會(huì)看不出漸變效果。centerX、centerY的取值其實(shí)是寬和高的百分比
實(shí)現(xiàn)效果

實(shí)現(xiàn)代碼: 取寬度的20%和高度的80%的位置,作為新的漸變?cè)c(diǎn)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:type="sweep"
android:startColor="#ff0000"
android:centerColor="#00ff00"
android:endColor="#0000ff"
android:centerX="0.2"
android:centerY="0.8"/>
</shape>
總結(jié)
以上所述是小編給大家介紹的Android實(shí)現(xiàn) Shape屬性gradient 漸變效果,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
Android ViewPager實(shí)現(xiàn)Banner循環(huán)播放
這篇文章主要為大家詳細(xì)介紹了Android ViewPager實(shí)現(xiàn)Banner循環(huán)播放,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Android學(xué)習(xí)之BottomSheetDialog組件的使用
BottomSheetDialog是底部操作控件,可在屏幕底部創(chuàng)建一個(gè)支持滑動(dòng)關(guān)閉視圖。本文將通過(guò)示例詳細(xì)講解它的使用,感興趣的小伙伴可以了解一下2022-06-06
Android開(kāi)發(fā)使用Handler的PostDelayed方法實(shí)現(xiàn)圖片輪播功能
這篇文章主要介紹了Android開(kāi)發(fā)使用Handler的PostDelayed方法實(shí)現(xiàn)圖片輪播功能,結(jié)合實(shí)例形式分析了Android基于Handler的PostDelayed方法實(shí)現(xiàn)圖片輪播功能的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
Android studio自定義對(duì)話(huà)框效果
這篇文章主要為大家詳細(xì)介紹了Android studio自定義對(duì)話(huà)框效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
Android游戲開(kāi)發(fā)學(xué)習(xí)①?gòu)椞∏驅(qū)崿F(xiàn)方法
這篇文章主要介紹了Android游戲開(kāi)發(fā)學(xué)習(xí)①?gòu)椞∏驅(qū)崿F(xiàn)方法,涉及Android通過(guò)物理引擎BallThread類(lèi)模擬小球運(yùn)動(dòng)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
詳解Android冷啟動(dòng)實(shí)現(xiàn)APP秒開(kāi)的方法
這篇文章給大家介紹的是Android冷啟動(dòng)實(shí)現(xiàn)APP秒開(kāi)的方法,對(duì)大家日常開(kāi)發(fā)APP還是很實(shí)用的,有需要的可以參考借鑒。2016-08-08
Android 使用AsyncTask實(shí)現(xiàn)斷點(diǎn)續(xù)傳
這篇文章主要介紹了Android 使用AsyncTask實(shí)現(xiàn)斷點(diǎn)續(xù)傳的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-05-05
Android shell命令行中過(guò)濾adb logcat輸出的方法
本文主要介紹Android shell命令行中過(guò)濾adb logcat輸出,這里詳細(xì)說(shuō)明了shell 命令過(guò)濾logcat 輸出內(nèi)容,有需要的小伙伴可以參考下2016-08-08
Android混合開(kāi)發(fā)教程之WebView的使用方法總結(jié)
WebView是一個(gè)基于webkit引擎、展現(xiàn)web頁(yè)面的控件,下面這篇文章主要給大家介紹了關(guān)于Android混合開(kāi)發(fā)教程之WebView的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧2018-05-05

