這篇文章主要介紹Android怎么實現Shape屬性gradient漸變效果,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
成都創新互聯公司專注于企業成都全網營銷、網站重做改版、南安網站定制設計、自適應品牌網站建設、H5網站設計、商城建設、集團公司官網建設、成都外貿網站制作、高端網站制作、響應式網頁設計等建站業務,價格優惠性價比高,為南安等各大城市提供網站開發制作服務。
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中漸變類型,線性漸變(默認)/放射漸變/掃描式漸變 android:angle="integer" //漸變角度,必須為45的倍數,0為從左到右,90為從上到下 android:centerX="float" //漸變中心X的相當位置,范圍為0~1 android:centerY="float" //漸變中心Y的相當位置,范圍為0~1 android:startColor="color" //漸變開始點的顏色 android:centerColor="color" //漸變中間點的顏色,在開始與結束點之間 android:endColor="color" //漸變結束點的顏色 android:gradientRadius="float" //漸變的半徑,只有當漸變類型為radial時才能使用 android:useLevel=["true" | "false"] /> //使用LevelListDrawable時就要設置為true。設為false時才有漸變效果
首先有三種漸變類型,分別是:linear(線性漸變)、radial(放射性漸變)、sweep(掃描式漸變)
</shape>
android:useLevel屬性通常不使用。該屬性用于指定是否將該shape當成一個LevelListDrawable來使用,默認值為false。
angle屬性確實只對線性漸變有效,其它兩種漸變方式都沒有任何動靜
centerX、centerY兩個屬性用于設置漸變的中心點位置,僅當漸變類型為放射漸變時有效。
2,Demo實現效果
我們使用三色漸變來看看這三種漸變方式都是怎么顯示的:(如果不使用centerColor屬性就是雙色漸變,這個屬性是可選的)
注意: 在構造放射性漸變時,要加上android:gradientRadius屬性(漸變半徑),即必須指定漸變半徑的大小才會起作用
【1】 線性漸變
實現效果

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】 放射性漸變
實現效果

實現代碼
<?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】 掃描式漸變
實現效果

實現代碼
<?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屬性修改漸變角度 (僅對線性漸變有效)
1)android:angle="integer" //漸變角度,必須為45的倍數,0為從左到右,90為從上到下
2)angle屬性確實只對線性漸變有效,其它兩種漸變方式都沒有任何動靜
實現效果:
實現代碼:
<?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兩個屬性用于設置漸變的中心點位置,僅當漸變類型為放射漸變時有效。
3)類型為分數或小數,不接受Dimension。默認值是0.5,有效值是0.0~1.0,超出該范圍后會看不出漸變效果。centerX、centerY的取值其實是寬和高的百分比
實現效果

實現代碼: 取寬度的20%和高度的80%的位置,作為新的漸變原點
<?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>
以上是“Android怎么實現Shape屬性gradient漸變效果”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注創新互聯行業資訊頻道!
分享文章:Android怎么實現Shape屬性gradient漸變效果
分享地址:http://www.yijiale78.com/article36/pcpjsg.html
成都網站建設公司_創新互聯,為您提供ChatGPT、手機網站建設、服務器托管、建站公司、靜態網站、商城網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯