我正在尝试使用9个补丁图像实现我自己的自定义进度条。我正在看Sound ProgressBar示例。 http://www.jagsaund.com/blog/2011/11/6/customizing-your-progress-bar-part-one.html
我有两个问题,我无法弄清楚为什么会发生这种情况:
无论我做什么,我的进度条都不是 layerdrawable,我不知道为什么会这样,所以我无法做到 获取对我想要的图层的引用并以语法方式进行更改。
9补丁图像开始拉伸到容器的整个宽度(relativelayout)不知道怎么告诉它不 自动缩放,以便我可以用语法控制它的宽度。
以下是代码snipets:
Drawable progressbar.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background"
android:drawable="@drawable/progressbar_bg" >
</item>
<item android:id="@+id/progress">
<bitmap
android:src="@drawable/progressbar"
android:tileMode="repeat" />
</item>
</layer-list>
Style.xml
<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>
XML布局文件中的自定义视图:
<com.custom.ahmad.views.NfcProgressBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/timeOutProgress"
android:layout_above="@+id/tvStart"
style="@style/Widget.ProgressBar.RegularProgressBar" />
最后自定义Progressbar类:
public class NfcProgressBar extends ProgressBar {
private String text = "";
private int textColor = Color.BLACK;
private float textSize = 15;
public NfcProgressBar(Context context) {
super(context);
}
public NfcProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NfcProgressBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected synchronized void onDraw(Canvas canvas) {
Drawable progressDrawable = this.getProgressDrawable();
Log.i("Testing", "progressDrawable : " + ((progressDrawable != null) ? progressDrawable : "null"));
Log.i("Testing", "progressDrawable instanceof LayerDrawable : " + ((progressDrawable instanceof LayerDrawable)? "true" : "false"));
if (progressDrawable != null && progressDrawable instanceof LayerDrawable) {
// Get the layer and do some programming work.
}
}