我有一个循环的进度条,它工作正常。但背景并没有出现,即使我已经为"背景"在xml可绘制文件中,进度也从圆的右侧开始(如图所示)。我希望它从顶部开始。
这是我的代码:
<ProgressBar
android:id="@+id/progressBarToday"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_centerInParent="true"
android:indeterminate="false"
android:max="60"
android:progress="0"
android:progressDrawable="@drawable/progressbar" />
progressbar.xml
<?xml version="1.0" encoding="UTF-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background"> <---not working!
<shape
android:innerRadiusRatio="3"
android:shape="ring"
android:thicknessRatio="7.0">
<solid android:color="@color/red"/>
</shape>
</item>
<item android:id="@android:id/progress">
<shape
android:innerRadiusRatio="3"
android:shape="ring"
android:thicknessRatio="7.0">
<solid android:color="@color/blue"/>
</shape>
</item>
</layer-list>
我使用以下代码来设置进度:
ProgressBar pb = (ProgressBar)FindViewById (Resource.Id.progressBarToday);
_progressBar.Progress = _progressCount;
为什么没有出现背景?我如何从顶部开始进步?
任何人都请帮忙,谢谢。
答案 0 :(得分:27)
您需要将以下属性添加到背景项:
android:useLevel="false"
像这样:
<?xml version="1.0" encoding="UTF-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background"> <---not working!
<shape
android:innerRadiusRatio="3"
android:shape="ring"
android:useLevel="false"
android:thicknessRatio="7.0">
<solid android:color="@color/red"/>
</shape>
</item>
<item android:id="@android:id/progress">
<shape
android:innerRadiusRatio="3"
android:shape="ring"
android:thicknessRatio="7.0">
<solid android:color="@color/blue"/>
</shape>
</item>
</layer-list>
答案 1 :(得分:5)
要从顶部开始,您可以使用“旋转
<item android:id="@android:id/progress">
<rotate
android:fromDegrees="-90"
android:toDegrees="-90">
<shape
android:innerRadiusRatio="3"
android:shape="ring"
android:thicknessRatio="7.0"
android:useLevel="true">
<solid android:color="@color/blue"/>
</shape>
</rotate>
</item>
答案 2 :(得分:4)
我得到了它的工作
<ProgressBar
android:id="@+id/progressBarToday"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_centerInParent="true"
android:indeterminate="false"
android:max="60"
android:progress="0"
android:background="@drawable/bg" // here is working background
android:progressDrawable="@drawable/progressbar" />
答案 3 :(得分:1)
是。我来不及但这对其他人有帮助。 此代码将返回位图图像,可以在ImageView或ImageButton中使用。
private Bitmap progressBar(int progress) {
Bitmap bitmap = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.parseColor("#FF0000"));
paint.setStrokeWidth(20);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
canvas.drawCircle(150, 150, 140, paint);
paint.setColor(Color.parseColor("#01aa01"));
paint.setStrokeWidth(16);
paint.setStyle(Paint.Style.STROKE);
final RectF oval = new RectF();
paint.setStyle(Paint.Style.FILL);
oval.set(10, 10, 290, 290);
canvas.drawArc(oval, 270, (progress * 360) / 100, true, paint);
paint.setStrokeWidth(1);
paint.setTextSize(100);
paint.setTextAlign(Align.CENTER);
paint.setColor(Color.parseColor("#ffffff"));
canvas.drawText("" + progress, 150, 150 + (paint.getTextSize() / 3),
paint);
return bitmap;
}