這篇文章主要介紹“Android中如何利用ProgressBar實(shí)現(xiàn)顏色漸變”,在日常操作中,相信很多人在Android中如何利用ProgressBar實(shí)現(xiàn)顏色漸變問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Android中如何利用ProgressBar實(shí)現(xiàn)顏色漸變”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、鳳縣網(wǎng)絡(luò)推廣、微信小程序開(kāi)發(fā)、鳳縣網(wǎng)絡(luò)營(yíng)銷、鳳縣企業(yè)策劃、鳳縣品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供鳳縣建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:muchs.cn首先,我們實(shí)現(xiàn)上面的布局,背景灰色,一個(gè)ProgressBar居中,一個(gè)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有一個(gè) indeterminate 屬相需要解釋一下:
一般的ProgressBar都是用于顯示加載進(jìn)度,如果我們直到當(dāng)前的具體進(jìn)度,那個(gè)這個(gè)屬性要設(shè)置為true,并設(shè)置正確的進(jìn)度,如果我們也不知道正確的進(jìn)度,則設(shè)置為false。
布局設(shè)置好了,下一步就是設(shè)置ProgressBar的漸變樣式,這里我們需要自定義一個(gè)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 表明這是一個(gè)旋轉(zhuǎn)的動(dòng)畫,并且該規(guī)定了開(kāi)始角度和結(jié)束角度,還有旋轉(zhuǎn)中心為圓心
內(nèi)層的shape定義了形狀為一個(gè)環(huán)(ring),其中有三個(gè)屬性:
<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中使用
接下來(lái)的 gradient 定義了漸變效果,規(guī)定了開(kāi)始結(jié)束的顏色,還規(guī)定漸變方式為掃描漸變
最后一步,我們通過(guò)一個(gè)ProgressBar的屬性給他設(shè)置我們上面定義的樣式:
android:indeterminateDrawable="@drawable/loading_drawable"
到此,關(guān)于“Android中如何利用ProgressBar實(shí)現(xiàn)顏色漸變”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
文章名稱:Android中如何利用ProgressBar實(shí)現(xiàn)顏色漸變-創(chuàng)新互聯(lián)
URL鏈接:http://muchs.cn/article32/deispc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、服務(wù)器托管、域名注冊(cè)、企業(yè)網(wǎng)站制作、自適應(yīng)網(wǎng)站、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容