1. 程式人生 > >android自定義ProgressBar 修改進度條樣式 出現setProgress無效問題

android自定義ProgressBar 修改進度條樣式 出現setProgress無效問題

在很多時候android系統提供的進度條樣式是不足以滿足我們的需求的,因此在大多數的時候,我們為了美觀,亦或者為了實現自己的功能,需要在系統的基礎上進行修改其樣式,當然你也可以自己從頭寫一個,不過個人覺得還是比較麻煩,好吧,廢話不多說了,進入正題。

昨天在做一個下載功能時,需要一個進度條顯示其進度,本來是很簡單的功能,以前也做過,本以為很容易就可以實現,卻因為一個細節,浪費了好幾個小時,因此就記錄一下,以防其他人遇到同樣的問題。

首先說一下我遇到的問題,自定義進度條樣式時,設定layer-list中時使用的是.9圖片,導致在java程式碼中設定setProgress無效問題,原因.9圖片直接填充整個進度條

,那問題來了,進度條樣式是不是不能用.9圖片呢?答案是否定的,先來看下具體的程式碼:

佈局檔案

<FrameLayout
        android:id="@+id/progress_layout"
        android:layout_width="match_parent"
        android:layout_height="38dp"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@id/bottom_share"
        android:layout_toRightOf="@id/bottom_favorites"
        android:background="@drawable/detail_btn">

        <ProgressBar
            android:id="@+id/pb_load_process"
            style="?android:attr/progressBarStyleHorizontal"
            android:progressDrawable="@drawable/progress_drawable"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:progress="0"
            android:max="100"
             />

        <TextView
            android:id="@+id/tv_load_process"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="暫停"
            android:textColor="#ffffff"
            android:textSize="18sp" />
    </FrameLayout>

progressBar應用樣式檔案

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background" android:drawable="@drawable/progress_bg"/>
    <item android:id="@android:id/progress" android:drawable="@drawable/progress_btn_normal"/>
</layer-list>
因為用不到第二進度所以就沒加,progress_btn_normal是.9圖片藍色的,progress_bg是黑色的圖片,先看下效果圖

 

我們可以看到下載到5%時的時候,進度圖片直接填充了整個進度條,好吧,其實從0%開始就是直接填充的整個進度條,我一開始以為是我有什麼引數設定錯了,把我以前寫的拿出來對比了半天,發現沒有什麼不一樣的,這時候就頭疼了,百度google出來的答案一大堆,一開始沒想到是圖片的問題,以為是哪邊設定錯誤導致setProgress無效,有說是需要用handler來更新進度,不能放到主執行緒,還有說這是ProgressBar的一個BUG等等,試了幾個都沒用,真是見了鬼了,後來實在沒轍了,就逛百度Google只要跟ProgressBar自定義樣式,setProgress有關的,我就點進去看,終於看到一個東西,說是在自定義樣式時用.9圖片需要這樣設定<nine-patch android:src="@drawable/progress_btn_normal"/> 然後我就想到不會吧,難道跟圖片有關?然後我就直接把設定進度條樣式的那段直接刪除,只留一個背景,用系統原本的進度條,發現恢復正常了,然後又將進度樣式圖片改成普通的圖片,我滴天,進度終於有用了,換回.9圖片,又是一樣的填充了,原來還真是圖片的原因。。。。好吧,然後就是一陣欲哭無淚,又犯了這麼低階的錯誤。。。。。

那麼自定義進度樣式是不是真的只能用普通圖片,不能用.9圖片?然後我就按照看到的那樣載入.9圖片

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background" android:drawable="@drawable/progress_bg"/>
    <item android:id="@android:id/progress" >
        <nine-patch android:src="@drawable/progress_btn_normal"/>
    </item>
</layer-list>
 結果。。。還是跟原來一樣沒有效果。

於是我想到了看看系統對話方塊的樣式是怎麼寫的

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dip" />
            <gradient
                    android:startColor="#ff9d9e9d"
                    android:centerColor="#ff5a5d5a"
                    android:centerY="0.75"
                    android:endColor="#ff747674"
                    android:angle="270"
            />
        </shape>
    </item>
    
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                        android:startColor="#80ffd300"
                        android:centerColor="#80ffb600"
                        android:centerY="0.75"
                        android:endColor="#a0ffcb00"
                        android:angle="270"
                />
            </shape>
        </clip>
    </item>
    
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                        android:startColor="#ffffd300"
                        android:centerColor="#ffffb600"
                        android:centerY="0.75"
                        android:endColor="#ffffcb00"
                        android:angle="270"
                />
            </shape>
        </clip>
    </item>
    
</layer-list>
以上就是系統對話方塊的樣式,我們可以看到一個clip的節點,這個以前沒接觸過,所以果斷查了一下,剪下(Clip)影象資源,使用剪下影象資源可以只顯示一部分影象,這種資源經常被用在進度條的製作上,原來這個就是用在進度條上的,以前還真不知道。。。。
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background" android:drawable="@drawable/progress_bg" />
    <item android:id="@android:id/progress">
        <clip android:drawable="@drawable/progress_btn_normal"/>
    </item>
</layer-list>
效果圖:

至此,問題就解決了,這其實沒有什麼技術含量,要是之前沒遇到過,肯定不會想到是圖片的原因,我們可能更多的是會去排查自己的程式碼。希望能給遇到同樣問題的人一些幫助,節省時間,在這種問題上浪費時間個人覺得是非常不值的。。。