1
+ using SkiaSharp ;
2
+ using SkiaSharp . Views . Forms ;
3
+ using Xamarin . Forms ;
4
+
5
+ namespace TemplateUI . Skia . Controls
6
+ {
7
+ public class SKBoxView : SKCanvasView
8
+ {
9
+ Color _color ;
10
+ CornerRadius _cornerRadius ;
11
+
12
+ public SKBoxView ( ) : base ( )
13
+ {
14
+ }
15
+
16
+ public static readonly BindableProperty ColorProperty =
17
+ BindableProperty . Create ( nameof ( Color ) , typeof ( Color ) , typeof ( SKBoxView ) , Color . Default , BindingMode . OneWay , null ,
18
+ propertyChanged : OnColorChanged ) ;
19
+
20
+ static void OnColorChanged ( BindableObject bindable , object oldValue , object newValue )
21
+ {
22
+ ( bindable as SKBoxView ) ? . UpdateColor ( ( Color ) newValue ) ;
23
+ }
24
+
25
+ public Color Color
26
+ {
27
+ get => ( Color ) GetValue ( ColorProperty ) ;
28
+ set => SetValue ( ColorProperty , value ) ;
29
+ }
30
+
31
+ public static readonly BindableProperty CornerRadiusProperty =
32
+ BindableProperty . Create ( nameof ( CornerRadius ) , typeof ( CornerRadius ) , typeof ( SKBoxView ) , new CornerRadius ( ) , BindingMode . OneWay , null ,
33
+ propertyChanged : OnCornerRadiusChanged ) ;
34
+
35
+ static void OnCornerRadiusChanged ( BindableObject bindable , object oldValue , object newValue )
36
+ {
37
+ ( bindable as SKBoxView ) ? . UpdateCornerRadius ( ( CornerRadius ) newValue ) ;
38
+ }
39
+
40
+ public CornerRadius CornerRadius
41
+ {
42
+ get => ( CornerRadius ) GetValue ( CornerRadiusProperty ) ;
43
+ set => SetValue ( CornerRadiusProperty , value ) ;
44
+ }
45
+
46
+ protected override void OnPaintSurface ( SKPaintSurfaceEventArgs args )
47
+ {
48
+ var surface = args . Surface ;
49
+ var canvas = surface . Canvas ;
50
+
51
+ canvas . Clear ( ) ;
52
+
53
+ using ( var paint = new SKPaint ( ) )
54
+ {
55
+ paint . Style = SKPaintStyle . Fill ;
56
+ paint . IsAntialias = true ;
57
+
58
+ SkiaSharp . SKPath path = CreateRoundedRectPath ( 0 , 0 , args . Info . Width , args . Info . Height , _cornerRadius ) ;
59
+
60
+ paint . Color = _color . ToSKColor ( ) ;
61
+ canvas . ClipPath ( path , SKClipOperation . Intersect , true ) ;
62
+ canvas . DrawPath ( path , paint ) ;
63
+ }
64
+ }
65
+
66
+ SkiaSharp . SKPath CreateRoundedRectPath ( int left , int top , int width , int height , CornerRadius cornerRadius )
67
+ {
68
+ var path = new SkiaSharp . SKPath ( ) ;
69
+ var skRoundRect = new SKRoundRect ( new SKRect ( left , top , width , height ) ) ;
70
+
71
+ SKPoint [ ] radii = new SKPoint [ 4 ]
72
+ {
73
+ new SKPoint ( ( float ) cornerRadius . TopLeft , ( float ) cornerRadius . TopLeft ) ,
74
+ new SKPoint ( ( float ) cornerRadius . TopRight , ( float ) cornerRadius . TopRight ) ,
75
+ new SKPoint ( ( float ) cornerRadius . BottomRight , ( float ) cornerRadius . BottomRight ) ,
76
+ new SKPoint ( ( float ) cornerRadius . BottomLeft , ( float ) cornerRadius . BottomLeft )
77
+ } ;
78
+
79
+ skRoundRect . SetRectRadii ( skRoundRect . Rect , radii ) ;
80
+ path . AddRoundRect ( skRoundRect ) ;
81
+ path . Close ( ) ;
82
+
83
+ return path ;
84
+ }
85
+
86
+ void UpdateColor ( Color color )
87
+ {
88
+ _color = color ;
89
+ InvalidateSurface ( ) ;
90
+ }
91
+
92
+ void UpdateCornerRadius ( CornerRadius cornerRadius )
93
+ {
94
+ _cornerRadius = cornerRadius ;
95
+ InvalidateSurface ( ) ;
96
+ }
97
+ }
98
+ }
0 commit comments