这是插件中的一个错误,在插件的Github的问题部分中标记
并且有一个人添加了一种解决方法,但我不确定这是否对您有用,如此处所示他所做的是
CircleView.cs
注意:注释 CornerRadius 属性。
public class CircleView : BoxView
{
//public static readonly BindableProperty CornerRadiusProperty = BindableProperty.Create(nameof(CornerRadius), typeof(double), typeof(CircleView), 0.0);
//public double CornerRadius
//{
// get { return (double)GetValue(CornerRadiusProperty); }
// set { SetValue(CornerRadiusProperty, value); }
//}
}
对于安卓:
CircleViewRenderer.cs
注意:为 CornerRadius (16) 添加了硬编码值
public class CircleViewRenderer : BoxRenderer
{
private float _cornerRadius;
private RectF _bounds;
private Path _path;
public CircleViewRenderer(Context context)
: base(context)
{
}
public static void Initialize() { }
protected override void OnElementChanged(ElementChangedEventArgs<BoxView> e)
{
base.OnElementChanged(e);
if (Element == null)
{
return;
}
var element = (CircleView)Element;
_cornerRadius = TypedValue.ApplyDimension(ComplexUnitType.Dip, (float)16, Context.Resources.DisplayMetrics);
}
protected override void OnSizeChanged(int w, int h, int oldw, int oldh)
{
base.OnSizeChanged(w, h, oldw, oldh);
if (w != oldw && h != oldh)
{
_bounds = new RectF(0, 0, w, h);
}
_path = new Path();
_path.Reset();
_path.AddRoundRect(_bounds, _cornerRadius, _cornerRadius, Path.Direction.Cw);
_path.Close();
}
public override void Draw(Canvas canvas)
{
canvas.Save();
canvas.ClipPath(_path);
base.Draw(canvas);
canvas.Restore();
}
}
对于 iOS:CircleViewRenderer.cs 注意:为 CornerRadius 添加硬编码值 (16) iOS 代码尚未测试。
public class CircleViewRenderer : BoxRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<BoxView> e)
{
base.OnElementChanged(e);
if (Element == null)
return;
Layer.MasksToBounds = true;
//Layer.CornerRadius = (float)((CircleView)Element).CornerRadius / 2.0f;
Layer.CornerRadius = (float)(16) / 2.0f;
}
}