2013-05-07 184 views
12

enter image description here多種顏色

我想創建這樣一個欄開始時進度爲零它會在顏色褪色,但和進步的推移它會成爲一部分明亮搜索欄或進度條(這最好我可以解釋一下)主要的是我想要酒吧同時顯示所有顏色。

+0

因爲你應該創建9個補丁映像。 – TheFlash 2013-05-07 07:38:34

+0

在我看來,最好創建一個自定義ProgressBar並根據它的進度設置不同的圖像/顏色。 – hardartcore 2013-05-07 09:04:41

+0

看看這可以幫助http://miroprocessordev.blogspot.in/2012/09/android-how-to-change-progress-bar.html – Android 2013-05-07 09:22:44

回答

16

夾你的 「上」 繪製:enter image description here
在你的 「關」 繪製:由enter image description here

使用res/drawable/custom_progress_drawable.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 

    <!-- Background --> 
    <item 
     android:id="@android:id/background" 
     android:drawable="@drawable/custom_progress_bar_off"/> 

    <!-- Secondary progress - this is optional --> 
    <item android:id="@android:id/secondaryProgress"> 
     <clip android:drawable="@drawable/custom_progress_bar_secondary" /> 
    </item> 

    <!-- Progress --> 
    <item android:id="@android:id/progress"> 
     <clip android:drawable="@drawable/custom_progress_bar_on" /> 
    </item> 

</layer-list> 

Activity,使用

Drawable progressDrawable = ResourcesCompat.getDrawable(getResources(), R.drawable.custom_progress_drawable, getTheme()); 
myProgressBar.setProgressDrawable(progressDrawable); 

或xml,請使用

android:progressDrawable="@drawable/custom_progress_drawable" 

而這裏的結果以XML使用android:max="10"時:

enter image description here

這是一個有點過,但調用setProgress()時,讓你可以使用setMax()的東西更像是10000,並做一些抵消計算它更乾淨。

+0

感謝您的好和簡單的解決方案... – 2013-05-17 10:34:04

+1

只是爲了添加一些信息http://developer.android.com/guide/topics/resources/drawable-resource.html#LayerList 根據這個,你應該把最大的10.000 注意:默認級別爲0,已完全剪切,因此圖像不可見。級別爲10,000時,圖像不會被剪切並完全可見。 – desgraci 2014-05-13 16:44:36

+0

如何將此ProgressBar設置爲垂直條而不是水平條? – 2016-03-25 21:35:51

0

就像已經建議,我認爲你應該去一個層次列表,然後設置多個drawables。 這個問題的主要問題是我需要調整大小。一個固定大小的解決方案將很容易實現。

6

終於!我去了一個任務,爲你解決這個問題,所以如果這足夠了,隨時給我這個賞金,哈哈。


嘗試在你的佈局使用此:

<View android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight=".20"/> 

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="0dip" 
       android:orientation="horizontal" 
       android:gravity="center" 
       android:layout_weight="0.62"> 
       <View android:layout_width="0dip" 
        android:layout_height="fill_parent" 
        android:layout_weight=".03"/> 
       <ProgressBar style="?android:attr/progressBarStyleHorizontal" 
        android:layout_width="0dip" 
        android:layout_height="fill_parent" 
        android:layout_weight="0.94" 
        android:progressDrawable="@drawable/progressmask" 
        android:progress="0" 
        android:max="10" 
        android:rotation="180"/> 
       <View android:layout_width="0dip" 
        android:layout_height="fill_parent" 
        android:layout_weight=".03"/> 
      </LinearLayout> 
    <View 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight=".18"/> 
</LinearLayout> 

它引用該繪製(progressmask.xml):

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:id="@android:id/background"> 
    <shape> 
     <corners android:radius="50dip" /> 
     <gradient android:startColor="#00000000" android:endColor="#00000000" android:angle="270" /> 
     <stroke android:width="1dp" android:color="#00000000" /> 
    </shape> 
</item> 
<item android:id="@android:id/progress"> 
    <clip> 
     <shape> 
      <corners android:radius="50dip" /> 
      <gradient android:startColor="#aa000000" android:endColor="#aa000000" 
       android:angle="270" /> 
      <stroke android:width="1dp" android:color="#00000000" /> 
     </shape> 
    </clip> 
</item> 
</layer-list> 

該圖像(colorprogress.png)

enter image description here

它所做的是將圖像設置爲包含進度條的線性佈局的背景。進度條爲圖像添加一個半透明的黑色遮罩,使其看起來燈光熄滅。

注意:爲了獲得這種效果,我必須用進度條猴子(即翻轉它,並設置它只有10個間隔。你將不得不做一些數學,以獲得進展與排隊圖像。即setprogress((100-trueprogress)/ 10)。對不起,我沒有爲你做這部分。

這是什麼看起來像進展50%(小x和三角形將消失在設備上)

enter image description here

我希望這回答您的問題!

+0

謝謝你的解決方案,但上面的答案很簡單..再次感謝.. – 2013-05-17 10:35:08

+1

啊,真的。我不得不承認,@UberPrinny的答案比我的好得多。 – dberm22 2013-05-17 11:17:31

0

實際上不能將進度條設置爲不同的顏色。但是,您只能使用on可繪製並獲得所需的效果。你可以應用一個圖層蒙版。我的意思是添加一個相對佈局,最初說dark grey,即漸變只有一種顏色是深灰色。現在,使用code以編程方式在左側設置漸變顏色。顯然,左邊的顏色將是透明的。瞭解更多關於Linear Gradients。就是這樣。您只需計算右側漸變開始的位置,而不是左側漸變(transparent)結束的位置。

此方法存在一些缺陷,可能無法在所有設備上使用。

完美的方法是創建多個.9png圖像,並且每次都以編程方式設置進度對話框的繪圖。