Android Xamarin-径向进度组件问题
我一直在尝试在我的应用程序上实现RadialProgress组件()。 我设法把它放在屏幕上,并改变了进度颜色,但我找不到改变圆圈内部颜色的方法 RadialProgressView对象本身有一个BackgroundTintMode字段,该字段采用DuffPorter.Mode,但每当我尝试设置背景色调模式时,应用程序就会中断,并显示以下消息(message=“没有名称为='setBackgroundTintMode'signature='(Landroid/graphics/PorterDuff$Mode;)的方法) 有什么办法可以满足我的要求吗Android Xamarin-径向进度组件问题,android,xamarin,Android,Xamarin,我一直在尝试在我的应用程序上实现RadialProgress组件()。 我设法把它放在屏幕上,并改变了进度颜色,但我找不到改变圆圈内部颜色的方法 RadialProgressView对象本身有一个BackgroundTintMode字段,该字段采用DuffPorter.Mode,但每当我尝试设置背景色调模式时,应用程序就会中断,并显示以下消息(message=“没有名称为='setBackgroundTintMode'signature='(Landroid/graphics/PorterDuf
谢谢!您可以尝试实现自定义的ViewRenderer,并访问底层的本机Android视图,以便根据需要修改它们。
有关“setBackgroundTintMode”方法的错误表明,您可能需要更新Xamarin平台,以确保最新的API可用是的,可以这样做。尽管不是以非常直接的方式,甚至不是以可维护的方式 首先,让我们深入了解一下
RadialProgressView
的绘图代码(由Xamarin Studio部件浏览器公开):
我们在这里注意到一些颜色,如bgCirclePaint
和bgBorderPaint
。如果我们能够更改这些变量的值,我们将能够更改绘制ProgressView的颜色
问题是RadialProgressView
没有公开字段–它们都是私有的,因此简单地从RadialProgressView
继承将不允许我们将它们设置为新值
但是,我们可以利用来更改这些私有字段,如下所示:
var textPaintMember = typeof(RadialProgressView).GetField("textPaint", BindingFlags.Instance | BindingFlags.NonPublic);
textPaintMember.SetValue(Instance, MyNewSuperCoolColorPaint);
通过将两者结合起来,我们可以创建一个新的、可定制的类,如下所示:
public class CustomizableRadialProgressView : RadialProgressView
{
public CustomizableRadialProgressView(Context context) : base(context)
{
}
public void SetTextColor(Color color)
{
var paint = new Paint();
paint.SetTypeface(Typeface.DefaultBold);
paint.Color = color;
paint.AntiAlias = true;
var textPaintMember = typeof(RadialProgressView).GetField("textPaint", BindingFlags.Instance | BindingFlags.NonPublic);
textPaintMember.SetValue(this, paint);
}
public void SetCircleColor(Color color)
{
var paint = new Paint();
paint.SetStyle(Paint.Style.Fill);
paint.Color = color;
paint.AntiAlias = true;
var circlePaintMember = typeof(RadialProgressView).GetField("bgCirclePaint", BindingFlags.Instance | BindingFlags.NonPublic);
circlePaintMember.SetValue(this, paint);
}
public void SetBorderColor(Color color)
{
var paint = new Paint();
paint.SetStyle(Paint.Style.Stroke);
paint.Color = color;
paint.AntiAlias = true;
var circlePaintMember = typeof(RadialProgressView).GetField("bgBorderPaint", BindingFlags.Instance | BindingFlags.NonPublic);
circlePaintMember.SetValue(this, paint);
}
public void SetProgressPackgroundColor(Color color)
{
var paint = new Paint();
paint.SetStyle(Paint.Style.Stroke);
paint.Color = color;
paint.AntiAlias = true;
var circlePaintMember = typeof(RadialProgressView).GetField("bgProgressPaint", BindingFlags.Instance | BindingFlags.NonPublic);
circlePaintMember.SetValue(this, paint);
}
}
这会让我们得到我们想要的结果:
注意:注意到我们不正确地使用私有字段可能是明智的:我们从它们所在的类之外对它们进行操作。如果Xamarin决定更改
RadialProgressView
的实现方式,甚至只重命名一个私有变量,我们的代码在运行时将失败。更好的方法是ch这个问题可能只是要求Xamarin提供你需要的getter/setter。但是,嘿,这样做太酷了;)你有什么解决办法吗?不幸的是,我没有Sarath。我还没有尝试Mina的建议,所以当我这样做时,我会让你知道。我已经在我的ios应用程序中实现了这个径向进度视图。但令人难忘毫无疑问,我没有找到任何改变其背景的选项。内部区域。我必须将其背景改为白色。您有没有想法改变Radial Progress view的背景。感谢您,我相信您必须访问底层组件并更改其属性,在安卓中,如果您可以将RadialProgressView转换为一个视图组,yo你可以通过getChildAt(int index)方法访问孩子们。@SARATH你能发布你的iOS解决方案吗?@Lijo你可以使用这个解决方案我们可以只更改戒指的中心部分吗?@SarathiOS:我不确定我是否理解你的问题。你想更改哪个部分?标签周围的视图(40)我必须改变这种观点。有可能吗?@SarathiOS:我又增加了两个颜色设置器。基本上,你需要将BorderColor设置回你想要的任何颜色,以及ProgressBackgroundColor。因此,通过将所有四种颜色设置为新值,这是可能的。有一个限制:在圆圈的最外侧,你会得到一个非常纤细的颜色(1px?)圆环颜色的边框,在任何情况下。所以最好像我一样选择隐藏这一点的颜色。谢谢您的时间和回复
public class CustomizableRadialProgressView : RadialProgressView
{
public CustomizableRadialProgressView(Context context) : base(context)
{
}
public void SetTextColor(Color color)
{
var paint = new Paint();
paint.SetTypeface(Typeface.DefaultBold);
paint.Color = color;
paint.AntiAlias = true;
var textPaintMember = typeof(RadialProgressView).GetField("textPaint", BindingFlags.Instance | BindingFlags.NonPublic);
textPaintMember.SetValue(this, paint);
}
public void SetCircleColor(Color color)
{
var paint = new Paint();
paint.SetStyle(Paint.Style.Fill);
paint.Color = color;
paint.AntiAlias = true;
var circlePaintMember = typeof(RadialProgressView).GetField("bgCirclePaint", BindingFlags.Instance | BindingFlags.NonPublic);
circlePaintMember.SetValue(this, paint);
}
public void SetBorderColor(Color color)
{
var paint = new Paint();
paint.SetStyle(Paint.Style.Stroke);
paint.Color = color;
paint.AntiAlias = true;
var circlePaintMember = typeof(RadialProgressView).GetField("bgBorderPaint", BindingFlags.Instance | BindingFlags.NonPublic);
circlePaintMember.SetValue(this, paint);
}
public void SetProgressPackgroundColor(Color color)
{
var paint = new Paint();
paint.SetStyle(Paint.Style.Stroke);
paint.Color = color;
paint.AntiAlias = true;
var circlePaintMember = typeof(RadialProgressView).GetField("bgProgressPaint", BindingFlags.Instance | BindingFlags.NonPublic);
circlePaintMember.SetValue(this, paint);
}
}