由于一个主题,你是试图把你自己的形象在ProgressBar那么你应该创建自定义ProgressBar这将帮助你设置自己的图像作为背景和进度图像。
在这里,我给你一个方法如何做自定义ProgressBar。创建一个XML处理background.png作为proress酒吧的背景和progress.png与层列表proress酒吧的进度指示器属性...命名XML作为progressbar.xml
android:id="@android:id/background"
android:drawable="@drawable/background"/>
android:id="@+id/progress"
android:drawable="@drawable/progress">
创建自定义ProgressBar命名为CustomProgressBar其中扩展ProgressBar。 CustomProgressBar.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创建自定义进度条样式属性...
true
repeat
3500
48dip
48dip
48dip
48dip
false
@drawable/progressbar
@android:drawable/progress_indeterminate_horizontal
1dip
10dip
添加自定义CustomProgressBar到您的XML。假设,你把CustomProgressBar.java文件中com.widgets.custom包......那么XML进度条会...
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 {
@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]);
}
}
}