2017-01-23 12 views
6

我嘗試實現水平不確定的ProgressBar,它將在末尾反轉其動畫。爲了澄清,我希望進度條從0到100進行動畫處理,然後從100回到0進行處理。 這裏是a video of the standard animation,我想在結尾處進行反轉。更改ProgressBar.Horizo​​ntal不確定行爲

根據documentation of ProgressBar這應該是可能的與XML,但我不能實現這一點。您可以設置重複(標)或週期(這是我想要的)

這裏是我的實現:

<ProgressBar 
    android:id="@+id/progressBar" 
    style="?android:attr/progressBarStyleHorizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="8dip" 
    android:indeterminate="true" 
    android:indeterminateOnly="true"  //tried "false" too 
    android:indeterminateBehavior="cycle" // type "reverse" is the one from linked video? 
    android:max="100" /> 

試圖與max-value,和不同的style-parents

然而,我發現值android:indeterminateDuration="[value]"並設置爲1秒,另一個爲10秒。最後,兩個進度加載器的長度相同,這讓我有了這樣的想法,這些風格可能被覆蓋在某個地方?!

有誰知道如何解決這個問題?

賞金更新:問題解決了,對於indeterminateBehaviour工作

+0

從'風格繼承什麼樣的價值觀=「機器人:ATTR/progressBarStyleHorizo​​ntal 「'? –

+0

@TimCastelijns:其實我不知道。這是標準的Android家長。我不知道,如何看看這個值: -/ – longilong

+0

你看看這個:http://stackoverflow.com/a/6450417/1185737 – KunalK

回答

0

你可以添加事件,其中,根據已知的因素會編程做一個工作示例:

progressBar.setIndeterminate(true); 

progressBar.setIndeterminate(false); 

考慮它是完全取決於您對所呈現數據的分析的樣式。

如果你知道它總是將是不確定的,有行爲的一貫風格預計添加這些XML屬性:

android:indeterminate="true" 
android:indeterminateDrawable="@drawable/progress_indeterminate_horizontal" 
android:indeterminateOnly="false" 
0

我找不到如何更改ProgressBar動畫師,所以我建議你用你自己的drawable去。

繪製進步爲矩形

在一個簡單的例子,讓我們的進步看起來像一個矩形:

res/drawable/vector_drawable_progress_indeterminate_horizontal.xml(名稱由core/res/res/drawable/vector_drawable_progress_indeterminate_horizontal.xml啓發)

<?xml version="1.0" encoding="utf-8"?> 
<vector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:height="10dp" 
    android:width="100dp" 
    android:viewportHeight="10" 
    android:viewportWidth="100" > 
     <group 
      android:name="rect1_grp" 
      android:scaleX="1" > 
      <path 
       android:pathData="M 0,0 h 100 v 10 h -100 Z" 
       android:fillColor="?attr/colorControlActivated" /> 
     </group> 
</vector> 

動畫水平刻度

當然,這個矩形根本沒有動畫,所以你想把它包裝在一個對象中它是水平尺度(scaleXrect1_grp)。

res/drawable/custom_progress.xml

<?xml version="1.0" encoding="utf-8"?> 
<animated-vector android:drawable="@drawable/vector_drawable_progress_indeterminate_horizontal" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <target 
     android:animation="@anim/progress_indeterminate_horizontal_rect1" 
     android:name="rect1_grp"/> 
</animated-vector> 

是剩下的唯一要做的,就是要有自己的動畫:

res/anim/progress_indeterminate_horizontal_rect1

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <objectAnimator 
     android:duration="1500" 
     android:propertyName="scaleX" 
     android:valueFrom="0" 
     android:valueTo="1" 
     android:valueType="floatType" 
     android:repeatMode="reverse" 
     android:repeatCount="infinite"/> 

</set> 

動畫線性地從0F至1F增長規模,並且以反向模式無限重複(避免有兩個動畫師序列:一個用於縮放增量,一個用於decr緩解)。

享受

現在,所有你需要做的就是在res/layout/activity.xml使用此繪製:

<ProgressBar 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="8dip" 
    android:indeterminate="true" 
    android:indeterminateDrawable="@drawable/custom_progress" /> 

剩下要做的

  • 我扯下了進展到最低限度。您可能想要繪製背景。您可能還想要恢復次要進度。
  • 您可以在動畫中自定義插補器。
+0

所以基本上,使用自定義動畫,而不是'android: indeterminateBehavior =「cycle」'和'android:indeterminateDuration =「10000」'?我想知道這些值是否可能被設備設置或樣式覆蓋,所以這是爲什麼我不能讓它們工作的原因? – longilong

0

另一種方法你也許可以遵循的是使用一個確定的風格,並設定值從0〜100,從100到0再次:

setProgress(100); 
    // sleep animation duration 
    setProgress(0); 
    // sleep animation duration 
    // repeat 

我認爲這個解決方案是小於優雅having a custom inderminateDrawble,因爲它需要有這個假進程更新。

0

添加到您的佈局,

 <ProgressBar 
     android:id="@+id/pbProcessing" 
     style="?android:attr/progressBarStyleHorizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/frame_getgas" 
     android:indeterminateOnly="true"/> 
2
<layer-list> 
<item android:id="@android:id/background"> 
    <shape> 
     <corners android:radius="2dip" /> 
     <gradient android:startColor="#43ffffff" android:centerColor="#43ffffff" android:centerY="0.75" android:endColor="#43ffffff" android:angle="270" /> 
    </shape> 
</item> 
<item android:id="@android:id/progress"> 
    <clip> 
     <shape> 
      <corners android:radius="2dip" /> 
      <gradient android:startColor="#fff" android:endColor="#fff" android:angle="270" /> 
     </shape> 
    </clip> 
</item> 

使用該繪製

Look at this picture

相關問題