這篇文章主要介紹“Android中如何利用ProgressBar實現(xiàn)顏色漸變”,在日常操作中,相信很多人在Android中如何利用ProgressBar實現(xiàn)顏色漸變問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Android中如何利用ProgressBar實現(xiàn)顏色漸變”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
首先,我們實現(xiàn)上面的布局,背景灰色,一個ProgressBar居中,一個TextView位于ProgressBar下方。
代碼如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="cn.codekong.androidloading.MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#de262a3b"> <ProgressBar android:id="@+id/loading" android:layout_width="60dp" android:layout_height="60dp" android:layout_centerInParent="true" android:indeterminate="false"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/loading" android:text="加載中" android:textColor="#ffffff" android:textSize="20sp" android:layout_centerHorizontal="true"/> </RelativeLayout> </RelativeLayout>
上面其他代碼都很好理解,只有ProgressBar有一個 indeterminate 屬相需要解釋一下:
一般的ProgressBar都是用于顯示加載進度,如果我們直到當前的具體進度,那個這個屬性要設(shè)置為true,并設(shè)置正確的進度,如果我們也不知道正確的進度,則設(shè)置為false。
布局設(shè)置好了,下一步就是設(shè)置ProgressBar的漸變樣式,這里我們需要自定義一個Drawable。
自定義的Drawable代碼如下:
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:toDegrees="1080.0"> <shape android:innerRadiusRatio="3" android:shape="ring" android:thicknessRatio="10" android:useLevel="false"> <gradient android:centerColor="#FFDC35" android:centerY="0.50" android:endColor="#CE0000" android:startColor="#FFFFFF" android:type="sweep"/> </shape> </rotate>
下面解釋一下上面的代碼:
外層的 rotate 表明這是一個旋轉(zhuǎn)的動畫,并且該規(guī)定了開始角度和結(jié)束角度,還有旋轉(zhuǎn)中心為圓心
內(nèi)層的shape定義了形狀為一個環(huán)(ring),其中有三個屬性:
<1> innerRadiusRatio 為外環(huán)半徑和內(nèi)徑的比值,比如外環(huán)半徑為30,內(nèi)環(huán)半徑為10,則比值為3
<2> thicknessRatio 為外環(huán)半徑與環(huán)的厚度的比值
<3> useLevel 如果為true,則可以在LevelListDrawable中使用
接下來的 gradient 定義了漸變效果,規(guī)定了開始結(jié)束的顏色,還規(guī)定漸變方式為掃描漸變
最后一步,我們通過一個ProgressBar的屬性給他設(shè)置我們上面定義的樣式:
android:indeterminateDrawable="@drawable/loading_drawable"
到此,關(guān)于“Android中如何利用ProgressBar實現(xiàn)顏色漸變”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
文章名稱:Android中如何利用ProgressBar實現(xiàn)顏色漸變-創(chuàng)新互聯(lián)
URL鏈接:http://sd-ha.com/article32/deispc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、服務(wù)器托管、域名注冊、企業(yè)網(wǎng)站制作、自適應(yīng)網(wǎng)站、動態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)