溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android中如何利用ProgressBar實(shí)現(xiàn)顏色漸變

發(fā)布時間:2022-04-11 15:48:21 來源:億速云 閱讀:516 作者:iii 欄目:編程語言

這篇文章主要介紹“Android中如何利用ProgressBar實(shí)現(xiàn)顏色漸變”,在日常操作中,相信很多人在Android中如何利用ProgressBar實(shí)現(xiàn)顏色漸變問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Android中如何利用ProgressBar實(shí)現(xiàn)顏色漸變”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

首先,我們實(shí)現(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都是用于顯示加載進(jìn)度,如果我們直到當(dāng)前的具體進(jìn)度,那個這個屬性要設(shè)置為true,并設(shè)置正確的進(jìn)度,如果我們也不知道正確的進(jìn)度,則設(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實(shí)現(xiàn)顏色漸變”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI