Xamarin.forms Xamarin形成Android框架渲染器和角半径

Xamarin.forms Xamarin形成Android框架渲染器和角半径,xamarin.forms,xamarin.android,Xamarin.forms,Xamarin.android,当我在Android中为Xamarin Forms Frame创建自定义渲染器时,CornerRadiusproperty会遇到麻烦。不管我怎么设置,它都不起作用。它总是画一个矩形 沙马林剂型(对照)- Xamarin形式(XAML)- 沙马林安卓- public class MyFrameRenderer : ViewRenderer<Controls.MyFrame, FrameRenderer> { public MyFrameRenderer(Context co

当我在Android中为Xamarin Forms Frame创建自定义渲染器时,
CornerRadius
property会遇到麻烦。不管我怎么设置,它都不起作用。它总是画一个矩形

沙马林剂型(对照)-

Xamarin形式(XAML)-


沙马林安卓-

public class MyFrameRenderer : ViewRenderer<Controls.MyFrame, FrameRenderer>
{
    public MyFrameRenderer(Context context) : base(context)
    {

    }

    protected override void OnElementChanged(ElementChangedEventArgs<Controls.MyFrame> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement != null)
        {
        }
    }
}
公共类MyFrameRenderer:ViewRenderer { 公共MyFrameRenderer(上下文):基础(上下文) { } 受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e) { 基础。一个要素发生变化(e); if(例如NewElement!=null) { } } } 如何设置
CornerRadius
属性使其具有圆角

谢谢

更新

Neumorphism是设计趋势,控制有两种阴影,黑暗和光明。若你们访问这个网站,你们可以想象我正在努力实现的目标


我使用Android的9补丁图像为矩形框架实现了类似的效果,但是,当我尝试为其提供
拐角半径
时,它没有起作用。

试试这个

将拐角半径设置为高度和宽度的一半,并将填充设置为0

 <Frame  HeightRequest="100" Padding="0" HorizontalOptions="Center" HasShadow="True" BackgroundColor="Accent" IsClippedToBounds="True" WidthRequest="100" CornerRadius="50">
            <Image Source="yourimage.png" Aspect="AspectFill"></Image>
 </Frame>

您可以尝试使用Nuget软件包在每个iOS和Android中实现它。看看它的功能


其效果是:

如果你想制作不同形状的图像,你可以使用它的Forms9Patch.image

我是怎么做到的

为视图创建了自定义渲染器。具有主色、暗影和浅影的属性-

public class NeumorphicCircleBackground : View
{
    public static readonly BindableProperty LightColorProperty =
        BindableProperty.Create("LightColor", typeof(Color), typeof(NeumorphicCircleBackground), null);

    public Color LightColor
    {
        get { return (Color)GetValue(LightColorProperty); }
        set { SetValue(LightColorProperty, value); }
    }

    public static readonly BindableProperty MainColorProperty =
        BindableProperty.Create("MainColor", typeof(Color), typeof(NeumorphicCircleBackground), null);

    public Color MainColor
    {
        get { return (Color)GetValue(MainColorProperty); }
        set { SetValue(MainColorProperty, value); }
    }

    public static readonly BindableProperty DarkColorProperty =
        BindableProperty.Create("DarkColor", typeof(Color), typeof(NeumorphicCircleBackground), null);

    public Color DarkColor
    {
        get { return (Color)GetValue(DarkColorProperty); }
        set { SetValue(DarkColorProperty, value); }
    }
}
和安卓视图-

protected override void OnDraw(Canvas canvas)
    {
        base.OnDraw(canvas);

        var h = canvas.Height;
        var w = canvas.Width;
        var size = Math.Min(h, w);

        float a = 0.1f * size;
        float b = 0.6f * size;
        float x0 = (w - size) / 2;
        float y0 = 0f;

        RectF ovalLight = new RectF();
        RectF ovalMain = new RectF();
        RectF ovalDark = new RectF();
        ovalLight.Set(x0, y0, x0 + b, y0 + b);
        ovalMain.Set(x0 + a, y0 + a, x0 + a + b, y0 + a + b);
        ovalDark.Set(x0 + 2 * a, y0 + 2 * a, x0 + 2 * a + b, y0 + 2 * a + b);

        var r = b / 2;

        var xl = x0 + b / 2;
        var yl = y0 + b / 2;
        var xd = x0 + 2 * a + b / 2;
        var yd = y0 + 2 * a + b / 2;
        var xm = x0 + a + b / 2;
        var ym = y0 + a + b / 2;

        var lightGradient = GetRadialPaint(xl, yl, r, _lightColor);
        var darkGradient = GetRadialPaint(xd, yd, r, _darkColor);
        var mainGradient = new Paint(PaintFlags.AntiAlias) { Color = _mainColor };

        if (_isCircle)
        {
            DrawCircle(canvas, xl, yl, r, lightGradient);
            DrawCircle(canvas, xd, yd, r, darkGradient);
            DrawCircle(canvas, xm, ym, r, mainGradient);
        }
        else
        {
            DrawRoundedRectangle(canvas, ovalLight, 50f, 50f, lightGradient);
            DrawRoundedRectangle(canvas, ovalDark, 50f, 50f, darkGradient);
            DrawRoundedRectangle(canvas, ovalMain, 50f, 50f, mainGradient);
        }
    }

我添加了更多属性,使其成为普通圆角矩形和完美圆。您可以根据需要进行修改。

您的要求是什么?框架默认提供角半径。您是否正在尝试绘制圆形框架?是的。使用Android的9补丁图像(Neumorphic shadow)。您好,您能否分享
frame
您想要的效果,我会检查一下。@GauravMathur Xamarin forms有一个关于9补丁图像的nuget包,您可以在每个iOS或Android平台上尝试一下。在Neumorphic中,左上阴影颜色不同,右下阴影不同。我使用9补丁只是为了阴影,没有别的。您的解决方案将只添加默认材质阴影。哦,抱歉。对于ios,你可以检查这个,也可以检查这个,没有问题。iOS很好,但我没有找到任何适合Android的资源。很好的发现,但我推出了自己的版本。非常感谢您的帮助。@GauravMathur很高兴您找到了解决方案,太好了!
<StackLayout Padding=" 10">
    <!-- Place new controls here -->
    <Label Text="Welcome to Xamarin.Forms!" 
       HorizontalOptions="Center"
       VerticalOptions="CenterAndExpand" />
    <Forms9Patch:Frame Padding="20"
                       HeightRequest="100"
                       WidthRequest="100"
                       OutlineColor="Blue"
                       OutlineWidth="3"
                       OutlineRadius="10"
                       HasShadow="True"
                       BorderRadius="50"
                       BackgroundColor="Gray">
        <!--<Label Text="Forms9Patch.Frame w/ OutlineWidth+OutlineRadius"
               TextColor="White"
               HorizontalOptions="Center"
               VerticalOptions="Center"
               FontSize="14" />-->

    </Forms9Patch:Frame>

</StackLayout>
public class NeumorphicCircleBackground : View
{
    public static readonly BindableProperty LightColorProperty =
        BindableProperty.Create("LightColor", typeof(Color), typeof(NeumorphicCircleBackground), null);

    public Color LightColor
    {
        get { return (Color)GetValue(LightColorProperty); }
        set { SetValue(LightColorProperty, value); }
    }

    public static readonly BindableProperty MainColorProperty =
        BindableProperty.Create("MainColor", typeof(Color), typeof(NeumorphicCircleBackground), null);

    public Color MainColor
    {
        get { return (Color)GetValue(MainColorProperty); }
        set { SetValue(MainColorProperty, value); }
    }

    public static readonly BindableProperty DarkColorProperty =
        BindableProperty.Create("DarkColor", typeof(Color), typeof(NeumorphicCircleBackground), null);

    public Color DarkColor
    {
        get { return (Color)GetValue(DarkColorProperty); }
        set { SetValue(DarkColorProperty, value); }
    }
}
protected override void OnDraw(Canvas canvas)
    {
        base.OnDraw(canvas);

        var h = canvas.Height;
        var w = canvas.Width;
        var size = Math.Min(h, w);

        float a = 0.1f * size;
        float b = 0.6f * size;
        float x0 = (w - size) / 2;
        float y0 = 0f;

        RectF ovalLight = new RectF();
        RectF ovalMain = new RectF();
        RectF ovalDark = new RectF();
        ovalLight.Set(x0, y0, x0 + b, y0 + b);
        ovalMain.Set(x0 + a, y0 + a, x0 + a + b, y0 + a + b);
        ovalDark.Set(x0 + 2 * a, y0 + 2 * a, x0 + 2 * a + b, y0 + 2 * a + b);

        var r = b / 2;

        var xl = x0 + b / 2;
        var yl = y0 + b / 2;
        var xd = x0 + 2 * a + b / 2;
        var yd = y0 + 2 * a + b / 2;
        var xm = x0 + a + b / 2;
        var ym = y0 + a + b / 2;

        var lightGradient = GetRadialPaint(xl, yl, r, _lightColor);
        var darkGradient = GetRadialPaint(xd, yd, r, _darkColor);
        var mainGradient = new Paint(PaintFlags.AntiAlias) { Color = _mainColor };

        if (_isCircle)
        {
            DrawCircle(canvas, xl, yl, r, lightGradient);
            DrawCircle(canvas, xd, yd, r, darkGradient);
            DrawCircle(canvas, xm, ym, r, mainGradient);
        }
        else
        {
            DrawRoundedRectangle(canvas, ovalLight, 50f, 50f, lightGradient);
            DrawRoundedRectangle(canvas, ovalDark, 50f, 50f, darkGradient);
            DrawRoundedRectangle(canvas, ovalMain, 50f, 50f, mainGradient);
        }
    }