-
Notifications
You must be signed in to change notification settings - Fork 621
Expand file tree
/
Copy pathDrawingFragment.cs
More file actions
201 lines (176 loc) · 5.16 KB
/
DrawingFragment.cs
File metadata and controls
201 lines (176 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
using System.Collections.Generic;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.OS;
using Android.Views;
using AndroidX.Fragment.App;
using Google.Android.Material.Button;
using SkiaSharp;
using SkiaSharp.Views.Android;
namespace SkiaSharpSample;
public class DrawingFragment : Fragment
{
private static readonly int[] SwatchIds = new[]
{
Resource.Id.colorBlack,
Resource.Id.colorRed,
Resource.Id.colorBlue,
Resource.Id.colorGreen,
Resource.Id.colorOrange,
Resource.Id.colorPurple,
};
private SKCanvasView skiaView;
private View selectedSwatch;
private Google.Android.Material.Slider.Slider brushSlider;
private readonly List<(SKPath Path, SKColor Color, float StrokeWidth)> strokes = new();
private SKPath currentPath;
private SKColor currentColor = SKColors.Black;
private SKColor canvasBackground = SKColors.White;
private float BrushSize => brushSlider?.Value ?? 4f;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.fragment_drawing, container, false);
// Resolve theme surface color for canvas background
var typedValue = new Android.Util.TypedValue();
if (Context.Theme.ResolveAttribute(Resource.Attribute.colorSurface, typedValue, true))
{
var c = new Color(typedValue.Data);
canvasBackground = new SKColor((byte)c.R, (byte)c.G, (byte)c.B, (byte)c.A);
}
// Resolve theme onSurface color as default drawing color
if (Context.Theme.ResolveAttribute(Resource.Attribute.colorOnSurface, typedValue, true))
{
var c = new Color(typedValue.Data);
currentColor = new SKColor((byte)c.R, (byte)c.G, (byte)c.B, (byte)c.A);
}
skiaView = view.FindViewById<SKCanvasView>(Resource.Id.skiaView);
skiaView.PaintSurface += OnPaintSurface;
skiaView.Touch += OnTouch;
// Wire up color swatches — resolve SKColor from each swatch's background
foreach (var viewId in SwatchIds)
{
var swatch = view.FindViewById<View>(viewId);
SKColor bgColor = SKColors.Black;
if (OperatingSystem.IsAndroidVersionAtLeast(24)
&& swatch.Background is GradientDrawable gd && gd.Color != null)
{
var c = new Color(gd.Color.DefaultColor);
bgColor = new SKColor((byte)c.R, (byte)c.G, (byte)c.B);
}
else if (swatch.Background is ColorDrawable cd)
{
bgColor = new SKColor((byte)cd.Color.R, (byte)cd.Color.G, (byte)cd.Color.B);
}
var captured = bgColor;
swatch.Click += (s, e) =>
{
currentColor = captured;
SetSelectedSwatch(swatch);
};
}
// Select first swatch by default
selectedSwatch = view.FindViewById<View>(Resource.Id.colorBlack);
SetSelectedSwatch(selectedSwatch);
// Brush size slider
brushSlider = view.FindViewById<Google.Android.Material.Slider.Slider>(Resource.Id.brushSlider);
// Clear button
var clearBtn = view.FindViewById<MaterialButton>(Resource.Id.btnClear);
clearBtn.Click += (s, e) =>
{
foreach (var (path, _, _) in strokes)
path.Dispose();
strokes.Clear();
currentPath?.Dispose();
currentPath = null;
skiaView?.Invalidate();
};
return view;
}
private void SetSelectedSwatch(View swatch)
{
// Reset previous selection
if (selectedSwatch != null)
{
selectedSwatch.ScaleX = 1.0f;
selectedSwatch.ScaleY = 1.0f;
selectedSwatch.Elevation = 0;
}
// Highlight selected with scale and elevation
swatch.ScaleX = 1.3f;
swatch.ScaleY = 1.3f;
swatch.Elevation = 8;
selectedSwatch = swatch;
}
private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
{
var canvas = e.Surface.Canvas;
canvas.Clear(canvasBackground);
if (skiaView.Width <= 0 || skiaView.Height <= 0)
return;
using var paint = new SKPaint
{
IsAntialias = true,
Style = SKPaintStyle.Stroke,
StrokeCap = SKStrokeCap.Round,
StrokeJoin = SKStrokeJoin.Round,
};
float sx = (float)e.Info.Width / skiaView.Width;
float sy = (float)e.Info.Height / skiaView.Height;
canvas.Scale(sx, sy);
foreach (var (path, color, strokeWidth) in strokes)
{
paint.Color = color;
paint.StrokeWidth = strokeWidth;
canvas.DrawPath(path, paint);
}
if (currentPath != null)
{
paint.Color = currentColor;
paint.StrokeWidth = BrushSize;
canvas.DrawPath(currentPath, paint);
}
}
private void OnTouch(object sender, View.TouchEventArgs e)
{
var x = e.Event.GetX();
var y = e.Event.GetY();
switch (e.Event.Action)
{
case MotionEventActions.Down:
currentPath = new SKPath();
currentPath.MoveTo(x, y);
skiaView.Invalidate();
break;
case MotionEventActions.Move:
currentPath?.LineTo(x, y);
skiaView.Invalidate();
break;
case MotionEventActions.Up:
case MotionEventActions.Cancel:
if (currentPath != null)
{
strokes.Add((currentPath, currentColor, BrushSize));
currentPath = null;
skiaView.Invalidate();
}
break;
}
e.Handled = true;
}
public override void OnDestroyView()
{
if (skiaView != null)
{
skiaView.PaintSurface -= OnPaintSurface;
skiaView.Touch -= OnTouch;
skiaView = null;
}
foreach (var (path, _, _) in strokes)
path.Dispose();
strokes.Clear();
currentPath?.Dispose();
currentPath = null;
base.OnDestroyView();
}
}