回答

0

相信對於這一點,你需要設置一個完整的樣式爲android:progressBarStyleHorizontal 你可以看到所有需要的值,如果你去http://android-holo-colors.com/,只是下載設置進度欄

+0

我設置到Android:progressBarStyleHorizo​​ntal但如何改變默認圖像挖掘 – Damir

+0

你應該能夠通過風格來設定圖像到你這是從該網站生成的。一旦將該屬性設置爲您的樣式,您將創建一個樣式,其父類爲android:Widget.ProgressBar.Horizo​​ntal。在值dir中的樣式和主題文件中爲您提供了必要的屬性 – GrouchyPanda

0

由於一個主題,你是試圖把你自己的形象在ProgressBar那麼你應該創建自定義ProgressBar這將幫助你設置自己的圖像作爲背景和進度圖像。

在這裏,我給你一個方法如何做自定義ProgressBar。創建一個XML處理background.png作爲proress酒吧的背景和progress.png層列表proress酒吧的進度指示器屬性...命名XML作爲progressbar.xml

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

    <item 
     android:id="@android:id/background" 
     <!--setting background.png as background of proressbar--> 
     android:drawable="@drawable/background"/> 
    <item 
     android:id="@+id/progress" 
     <!--setting progress.png as progress drawable of proressbar--> 
     android:drawable="@drawable/progress"> 
    </item> 

</layer-list> 

創建自定義ProgressBar命名爲CustomProgressBar其中擴展ProgressBarCustomProgressBar.java

public class CustomProgressBar extends ProgressBar { 

    public CustomProgressBar(Context context) { 
     super(context); 
    } 

    public CustomProgressBar(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public CustomProgressBar(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    protected synchronized void onDraw(Canvas canvas) { 
     // update the size of the progress bar and overlay 
     updateProgressBar(); 

     // paint the changes to the canvas 
     super.onDraw(canvas); 
    } 

    @Override 
    public synchronized void setProgress(int progress) { 
     super.setProgress(progress); 

     // the setProgress super will not change the details of the progress bar 
     // anymore so we need to force an update to redraw the progress bar 
     invalidate(); 
    } 

    private float getScale(int progress) { 
     float scale = getMax() > 0 ? (float) progress/(float) getMax() : 0; 

     return scale; 
    } 

    private void updateProgressBar() { 
     Drawable progressDrawable = getProgressDrawable(); 

     if (progressDrawable != null && progressDrawable instanceof LayerDrawable) { 
      LayerDrawable d = (LayerDrawable) progressDrawable; 

      final float scale = getScale(getProgress()); 

      // get the progress bar and update it's size 
      Drawable progressBar = d.findDrawableByLayerId(R.id.progress); 

      final int width = d.getBounds().right - d.getBounds().left; 

      if (progressBar != null) { 
       Rect progressBarBounds = progressBar.getBounds(); 
       progressBarBounds.right = progressBarBounds.left + (int) (width * scale + 0.5f); 
       progressBar.setBounds(progressBarBounds); 
      } 

     } 
    } 
} 

styles.xml創建自定義進度條樣式屬性...

<resources> 

    <style name="Widget"></style> 

    <style name="Widget.ProgressBar"> 
     <item name="android:indeterminateOnly">true</item> 
     <item name="android:indeterminateBehavior">repeat</item> 
     <item name="android:indeterminateDuration">3500</item> 
     <item name="android:minWidth">48dip</item> 
     <item name="android:maxWidth">48dip</item> 
     <item name="android:minHeight">48dip</item> 
     <item name="android:maxHeight">48dip</item> 
    </style> 

    <style name="Widget.ProgressBar.RegularProgressBar"> 
     <item name="android:indeterminateOnly">false</item> 
     <item name="android:progressDrawable">@drawable/progressbar</item> 
     <item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item> 
     <item name="android:minHeight">1dip</item> 
     <item name="android:maxHeight">10dip</item> 
    </style> 

</resources> 

添加自定義CustomProgressBar到您的XML。假設,你把CustomProgressBar.java文件中com.widgets.custom包......那麼XML進度條會...

<com.widgets.custom.CustomProgressBar 
    android:id="@+id/regularprogressbar" 
    style="@style/Widget.ProgressBar.RegularProgressBar" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="50dip" /> 

在你的Activity類初始化定義進度欄和根據需要使用。但是我給演示活動類更好地瞭解...

public class ProgressBarActivity extends Activity { 

    private SaundProgressBar mRegularProgressBar; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     mRegularProgressBar = (SaundProgressBar) findViewById(R.id.regularprogressbar); 

     new UpdateBarTask().execute(); 
    } 

    private class UpdateBarTask extends AsyncTask<Void, Integer, Void> { 

     @Override 
     protected Void doInBackground(Void... params) { 

      int max = mRegularProgressBar.getMax(); 
      for (int i = 0; i <= max; i++) { 
       try { 
        // update every second 
        Thread.sleep(100); 
       } catch (InterruptedException e) { 

       } 

       publishProgress(i); 
      } 

      return null; 
     } 

     @Override 
     protected void onProgressUpdate(Integer... values) { 
      mRegularProgressBar.setProgress(values[0]); 
     } 
    } 
} 
相關問題