我知道这是一个老帖子,但像我这样的一些人在搜索圆形进度时会遇到这个主题。
Andreas Hennig制作了一个很好的插件,适用于iOS和Android,您可以从nuget安装它:Progress Ring Control Plugin
XAML
添加XAML命名空间:
xmlns:control="clr-namespace:ProgressRingControl.Forms.Plugin;assembly=ProgressRing.Forms.Plugin"
添加xaml:
<control:ProgressRing RingThickness="20" Progress="0.5"/>
您可以在以下位置找到它:
我的旅行倒计时是一个Xamarin.Forms示例,展示如何使用Xamarin.Forms创建好看的UI。此示例基于Victoria Sgarro设计的倒计时器。
https://devblogs.microsoft.com/xamarin/using-custom-controls-in-xamarin-forms-on-android/
官方文档中关于Xamarin.Forms自定义渲染器的内容可以在https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/找到。来自https://github.com/billreiss/xamlnative/tree/master/XamarinForms/CircularProgress的代码
public class CircularProgress : Grid
{
View progress1;
View progress2;
View background1;
View background2;
public CircularProgress()
{
var baseUrl = "https://github.com/billreiss/xamlnative/raw/master/XamarinForms/CircularProgress/CircularProgress/CircularProgress.Droid/Resources/drawable/";
progress1 = CreateImage($"{baseUrl}progress_done.png");
background1 = CreateImage($"{baseUrl}progress_pending.png");
background2 = CreateImage($"{baseUrl}progress_pending.png");
progress2 = CreateImage($"{baseUrl}progress_done.png");
HandleProgressChanged(1, 0);
}
private View CreateImage(string imageName)
{
var img = new Image();
img.Source = ImageSource.FromUri(new Uri(imageName));
this.Children.Add(img);
return img;
}
public static BindableProperty ProgressProperty =
BindableProperty.Create(nameof(Progress), typeof(double), typeof(CircularProgress), 0d, propertyChanged: ProgressChanged);
private static void ProgressChanged(BindableObject bindable, object oldValue, object newValue)
{
var c = bindable as CircularProgress;
c.HandleProgressChanged(Clamp((double)oldValue, 0, 1), Clamp((double)newValue, 0, 1));
}
static double Clamp(double value, double min, double max)
{
if (value <= max && value >= min) return value;
else if (value > max) return max;
else return min;
}
private void HandleProgressChanged(double oldValue, double p)
{
if (p < .5)
{
if (oldValue >= .5)
{
// this code is CPU intensive so only do it if we go from >=50% to <50%
background1.IsVisible = true;
progress2.IsVisible = false;
background2.Rotation = 180;
progress1.Rotation = 0;
}
double rotation = 360 * p;
background1.Rotation = rotation;
}
else
{
if (oldValue < .5)
{
// this code is CPU intensive so only do it if we go from <50% to >=50%
background1.IsVisible = false;
progress2.IsVisible = true;
progress1.Rotation = 180;
}
double rotation = 360 * p;
background2.Rotation = rotation;
}
}
public double Progress
{
get { return (double)this.GetValue(ProgressProperty); }
set { SetValue(ProgressProperty, value); }
}
}