因为我对非常陌生,所以Xamarin 世界及其控件是新手。我想在单声道触摸应用程序中添加圈子以显示工作进度。为了显示进度,我必须在圆中标记一个弧。如果可能的话,任何人都可以帮助我提供示例代码。等待答案,非常感谢。
最佳答案
using System;
using UIKit;
using CoreGraphics;
namespace CircleTest.Touch
{
public class CircleGraph : UIView
{
int _radius = 10;
int _lineWidth = 10;
nfloat _degrees = 0.0f;
UIColor _backColor = UIColor.FromRGB(46, 60, 76);
UIColor _frontColor = UIColor.FromRGB(234, 105, 92);
//FromRGB (234, 105, 92);
public CircleGraph (CGRect frame, int lineWidth, nfloat degrees)
{
_lineWidth = lineWidth;
_degrees = degrees;
this.Frame = new CGRect(frame.X, frame.Y, frame.Width, frame.Height);
this.BackgroundColor = UIColor.Clear;
}
public CircleGraph (CGRect frame, int lineWidth, UIColor backColor, UIColor frontColor)
{
_lineWidth = lineWidth;
this.Frame = new CGRect(frame.X, frame.Y, frame.Width, frame.Height);
this.BackgroundColor = UIColor.Clear;
}
public override void Draw (CoreGraphics.CGRect rect)
{
base.Draw (rect);
using (CGContext g = UIGraphics.GetCurrentContext ()) {
_radius = (int)( (this.Bounds.Width) / 3) - 8;
DrawGraph(g, this.Bounds.GetMidX(), this.Bounds.GetMidY());
};
}
public void DrawGraph(CGContext g,nfloat x0,nfloat y0) {
g.SetLineWidth (_lineWidth);
// Draw background circle
CGPath path = new CGPath ();
_backColor.SetStroke ();
path.AddArc (x0, y0, _radius, 0, 2.0f * (float)Math.PI, true);
g.AddPath (path);
g.DrawPath (CGPathDrawingMode.Stroke);
// Draw overlay circle
var pathStatus = new CGPath ();
_frontColor.SetStroke ();
pathStatus.AddArc(x0, y0, _radius, 0, _degrees * (float)Math.PI, false);
g.AddPath (pathStatus);
g.DrawPath (CGPathDrawingMode.Stroke);
}
}
}
实际上,这是iam真正应该做的。它为我工作
这就是它的样子。您可以像创建类文件一样创建它,并且只需将其分配给UIView。
有关更多引用,您可以使用此示例项目Pi Graph
[编辑]:
Draw
方法最初将View.Frame
x,y传递给DrawGraph
方法。这应该是View.Bounds
(上面进行了修改以反射(reflect)这一点)。请记住,框架x,y是指所包含的 super View ,并且范围是指当前 View 。如果在0,0处添加 View ,则此方法可行,但是一旦您开始在UI中移动,它就会消失。绘制圆弧时,传递给AddArc的x,y值必须引用当前 View 而不是父父 View 。