Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

xamarin.forms - How to make a progress bar with rounded corners in Xamarin Forms using Custom Renderer

I want to display progress bar with rounded corners in Xamarin forms. I was able to do it in iOS using a custom renderer:

class CustomProgressBarRenderer: ProgressBarRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e)
    {
        base.OnElementChanged(e);
        Control.ProgressTintColor = Color.FromRgb(255, 201, 74).ToUIColor();
    }

    public override void LayoutSubviews()
    {
        base.LayoutSubviews();
        var X = 1.0f;
        var Y = 7.0f;
        CGAffineTransform transform = CGAffineTransform.MakeScale(X, Y);
        this.Transform = transform;
        this.ClipsToBounds = true;
        this.Layer.MasksToBounds = true;
        this.Layer.CornerRadius = 5; // This is for rounded corners.
    }
}

How can I achieve the same in Android using Custom Renderer. This is how I want the progress bar(rounded corners) to look.

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

How to make a progress bar with rounded corners in Xamarin forms

You could add a Drawable on your android ProgressBar to implement this feature.

Set the style for your ProgressBar , Drawablear_color.xml :

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--set progressbar backgound style-->
    <item
        android:id="@android:id/background"
        android:drawable="@drawable/progress_bar_background" />

    <!--set progressbar schedule style-->
    <item android:id="@android:id/progress">
        <scale
            android:drawable="@drawable/shape_progressbar_progress"
            android:scaleWidth="100%" />
    </item>

</layer-list>

Drawableprogress_bar_background.xml :

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="20dp" />
    <solid android:color="#FFCBC2C2" />
</shape>

Drawableshape_progressbar_progress.xml :

<shape xmlns:android="http://schemas.android.com/apk/res/android">
   <corners android:radius="20dp" />
   <solid android:color="#3F8CC4" />
   <stroke
       android:width="0dp"
       android:color="#FFFFFF"></stroke>
</shape>

Using it in your CustomProgressBarRenderer :

public class CustomProgressBarRenderer : ProgressBarRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e)
    {
        base.OnElementChanged(e);

        if(Control != null)
        {
            var progressBar = Control as Android.Widget.ProgressBar;
            //below is now deprecated
            //var draw = Resources.GetDrawable(Resource.Drawable.bar_color);
            var draw = Context.GetDrawable(Resource.Drawable.bar_color);
            progressBar.ProgressDrawable = draw;
        }
    }
}

Effect :

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...