Skip to content

Commit c98a206

Browse files
committed
测试自定义的橡皮擦控件
1 parent a6bb2ff commit c98a206

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
using Avalonia;
8+
using Avalonia.Controls;
9+
using Avalonia.Layout;
10+
using Avalonia.Media;
11+
12+
using DotNetCampus.Inking.Erasing;
13+
14+
namespace AvaloniaInkCanvasDemo.Views.ErasingView;
15+
16+
internal class CustomEraserView : Control, IEraserView
17+
{
18+
public CustomEraserView()
19+
{
20+
Path1 = Geometry.Parse("M0,5.0093855C0,2.24277828,2.2303666,0,5.00443555,0L24.9955644,0C27.7594379,0,30,2.23861485,30,4.99982044L30,17.9121669C30,20.6734914,30,25.1514578,30,27.9102984L30,40.0016889C30,42.7621799,27.7696334,45,24.9955644,45L5.00443555,45C2.24056212,45,0,42.768443,0,39.9906145L0,5.0093855z");
21+
Path1FillBrush = new SolidColorBrush(new Color(0x33, 0, 0, 0));
22+
23+
//Path2 = Geometry.Parse("M20,29.1666667L20,16.1666667C20,15.3382395 19.3284271,14.6666667 18.5,14.6666667 17.6715729,14.6666667 17,15.3382395 17,16.1666667L17,29.1666667C17,29.9950938 17.6715729,30.6666667 18.5,30.6666667 19.3284271,30.6666667 20,29.9950938 20,29.1666667z M13,29.1666667L13,16.1666667C13,15.3382395 12.3284271,14.6666667 11.5,14.6666667 10.6715729,14.6666667 10,15.3382395 10,16.1666667L10,29.1666667C10,29.9950938 10.6715729,30.6666667 11.5,30.6666667 12.3284271,30.6666667 13,29.9950938 13,29.1666667z");
24+
//Path2FillBrush = new SolidColorBrush(new Color(0x26, 0, 0, 0));
25+
26+
Path3FillBrush = new SolidColorBrush(new Color(0xFF, 0xF2, 0xEE, 0xEB));
27+
28+
var bounds = Path1.Bounds;//.Union(Path2.Bounds);
29+
Width = bounds.Width;
30+
Height = bounds.Height;
31+
32+
HorizontalAlignment = HorizontalAlignment.Left;
33+
VerticalAlignment = VerticalAlignment.Top;
34+
IsHitTestVisible = false;
35+
36+
var translateTransform = new TranslateTransform();
37+
_translateTransform = translateTransform;
38+
var scaleTransform = new ScaleTransform();
39+
_scaleTransform = scaleTransform;
40+
var transformGroup = new TransformGroup();
41+
transformGroup.Children.Add(_scaleTransform);
42+
transformGroup.Children.Add(_translateTransform);
43+
RenderTransform = transformGroup;
44+
45+
_currentEraserSize = new Size(Width, Height);
46+
}
47+
48+
private readonly TranslateTransform _translateTransform;
49+
50+
private readonly ScaleTransform _scaleTransform;
51+
52+
private Geometry Path1 { get; }
53+
private IBrush Path1FillBrush { get; }
54+
55+
//private Geometry Path2 { get; }
56+
//private IBrush Path2FillBrush { get; }
57+
58+
private IBrush Path3FillBrush { get; }
59+
60+
private Size _currentEraserSize;
61+
62+
public void Move(Point position)
63+
{
64+
_translateTransform.X = position.X - _currentEraserSize.Width / 2;
65+
_translateTransform.Y = position.Y - _currentEraserSize.Height / 2;
66+
}
67+
68+
public void SetEraserSize(Size size)
69+
{
70+
_scaleTransform.ScaleX = size.Width / Width;
71+
_scaleTransform.ScaleY = size.Height / Height;
72+
73+
_currentEraserSize = size;
74+
}
75+
76+
public override void Render(DrawingContext context)
77+
{
78+
context.DrawGeometry(Path1FillBrush, null, Path1);
79+
//context.DrawGeometry(Path2FillBrush, null, Path2);
80+
context.DrawRectangle(Path3FillBrush, null, new RoundedRect(new Rect(1, 1, 28, 43), 4));
81+
}
82+
}

demo/AvaloniaInkCanvasDemo/Views/MainView.axaml.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using Avalonia.Controls;
22
using Avalonia.Interactivity;
33
using Avalonia.Skia;
4+
using AvaloniaInkCanvasDemo.Views.ErasingView;
45
using DotNetCampus.Inking;
6+
using DotNetCampus.Inking.Erasing;
57
using DotNetCampus.Inking.StrokeRenderers.WpfForSkiaInkStrokeRenderers;
68
using SkiaSharp;
79

@@ -12,6 +14,8 @@ public partial class MainView : UserControl
1214
public MainView()
1315
{
1416
InitializeComponent();
17+
var settings = InkCanvas.AvaloniaSkiaInkCanvas.Settings;
18+
settings.EraserViewCreator = new DelegateEraserViewCreator(() => new CustomEraserView());
1519
}
1620

1721
private void PenModeButton_OnClick(object? sender, RoutedEventArgs e)

0 commit comments

Comments
 (0)