亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android簡單實(shí)現(xiàn)一個(gè)顏色漸變的ProgressBar的方法

 更新時(shí)間:2017年12月02日 08:36:50   作者:冰鑒IT  
本篇文章主要介紹了Android簡單實(shí)現(xiàn)一個(gè)顏色漸變的ProgressBar的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

今天看一個(gè)教程,看到一個(gè)顏色漸變的ProgressBar,覺得有點(diǎn)意思,所以記錄一番。
下面這個(gè)是效果圖

顏色漸變的ProgressBar

看到效果圖可能會(huì)給人一種使用了高端技術(shù)的感覺,其實(shí)這個(gè)沒有那么高深,我們只是簡單改變了ProgressBar的樣式即可實(shí)現(xiàn),下面說說實(shí)現(xiàn)方式。

首先我們簡單分析一下:

1 . 上面的樣式只是實(shí)現(xiàn)了顏色漸變,但它旋轉(zhuǎn)和呈現(xiàn)的方式仍然是一個(gè)圓形的ProgressBar。

2 . 這個(gè)ProgressBar實(shí)現(xiàn)了顏色漸變,我們就需要用到gradient,這個(gè)也比較簡單,只要我們配置開始,中間,結(jié)束顏色即可實(shí)現(xiàn)
明白了上面兩點(diǎn)我們就開始寫代碼。

首先,我們實(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ī)定了開始角度和結(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中使用

接下來的 gradient 定義了漸變效果,規(guī)定了開始結(jié)束的顏色,還規(guī)定漸變方式為掃描漸變

最后一步,我們通過一個(gè)ProgressBar的屬性給他設(shè)置我們上面定義的樣式:

android:indeterminateDrawable="@drawable/loading_drawable"

經(jīng)過上面的步驟我們就實(shí)現(xiàn)了一個(gè)簡單的漸變的ProgressBar,是不是超級(jí)簡單,希望可以幫助到需要的人。

源碼地址: https://github.com/codekongs/Android-Learning/tree/master/AndroidLoading

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論