圆形 ProgressBar 背景:Android Xamarin

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22657086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:18:36  来源:igfitidea点击:

Circular ProgressBar Background: Android Xamarin

androidprogress-barxamarin.androidxamarinandroid-progressbar

提问by Erma Isabel

I have a circular progress-bar and it works fine.But Background is not appearing, even though i have given shape for "background" in the xml drawable file and also progress starts from the right side of the circle (as shown in the figure). I want it to make it from the top.

我有一个圆形进度条,它工作正常。但是背景没有出现,即使我在 xml drawable 文件中为“背景”指定了形状,并且进度从圆圈的右侧开始(如图所示) )。我希望它从顶部开始。

enter image description here

在此处输入图片说明

Here is my code:

这是我的代码:

 <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

    <?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>

I use following code to set progress:

我使用以下代码来设置进度:

    ProgressBar pb = (ProgressBar)FindViewById (Resource.Id.progressBarToday);  
    _progressBar.Progress = _progressCount;

Why the background is not appearing? and How can i make the progress start from the top?

为什么背景没有出现?以及如何从头开始取得进展?

Anyone please help, Thanks.

哪位大神帮忙看看,谢谢。

采纳答案by Erma Isabel

I got it working

我让它工作了

     <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" />

回答by Thomas

You need to add the following attribute to the background item:

您需要将以下属性添加到背景项中:

android:useLevel="false"

Like so:

像这样:

<?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>

回答by user3728055

To start from the top, you can use "rotate

要从顶部开始,您可以使用“旋转

<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>

回答by Nandan Kumar Singh

Yes. I am too late but this may help other. This code will return a bitmap image and can be used in a ImageView or ImageButton .

是的。我为时已晚,但这可能对其他人有所帮助。此代码将返回一个位图图像,并且可以在 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;
}