ImageViewer 鼠标滚动最小缩放比例从原图的0.1倍改为渲染区域的0.1倍

This commit is contained in:
杨劼
2024-05-30 16:44:14 +08:00
parent 68c55dd331
commit 4239a4ffe2
3 changed files with 18 additions and 2 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 MiB

View File

@@ -21,7 +21,7 @@
Name="viewer" Name="viewer"
Width="600" Width="600"
Height="300" Height="300"
Source="../Assets/WORLD.png"> Source="../Assets/3x.png">
<u:ImageViewer.Overlayer> <u:ImageViewer.Overlayer>
<Grid <Grid
HorizontalAlignment="Stretch" HorizontalAlignment="Stretch"

View File

@@ -101,6 +101,8 @@ public class ImageViewer: TemplatedControl
set => SetValue(StretchProperty, value); set => SetValue(StretchProperty, value);
} }
private double _minScale = 0.1;
static ImageViewer() static ImageViewer()
{ {
FocusableProperty.OverrideDefaultValue<ImageViewer>(true); FocusableProperty.OverrideDefaultValue<ImageViewer>(true);
@@ -161,12 +163,21 @@ public class ImageViewer: TemplatedControl
_image.Height = size.Height; _image.Height = size.Height;
} }
Scale = GetScaleRatio(width/size.Width, height/size.Height, this.Stretch); Scale = GetScaleRatio(width/size.Width, height/size.Height, this.Stretch);
_minScale = Math.Min(width * 0.1 / size.Width, height * 0.1 / size.Height);
} }
private void OnStretchChanged(AvaloniaPropertyChangedEventArgs args) private void OnStretchChanged(AvaloniaPropertyChangedEventArgs args)
{ {
var stretch = args.GetNewValue<Stretch>(); var stretch = args.GetNewValue<Stretch>();
Scale = GetScaleRatio(Width / _image!.Width, Height / _image!.Height, stretch); Scale = GetScaleRatio(Width / _image!.Width, Height / _image!.Height, stretch);
if(_image is { })
{
_minScale = Math.Min(Width * 0.1 / _image.Width, Height * 0.1 / _image.Height);
}
else
{
_minScale = 0.1;
}
} }
private double GetScaleRatio(double widthRatio, double heightRatio, Stretch stretch) private double GetScaleRatio(double widthRatio, double heightRatio, Stretch stretch)
@@ -203,6 +214,11 @@ public class ImageViewer: TemplatedControl
_image.Width = size.Width; _image.Width = size.Width;
_image.Height = size.Height; _image.Height = size.Height;
Scale = GetScaleRatio(width/size.Width, height/size.Height, this.Stretch); Scale = GetScaleRatio(width/size.Width, height/size.Height, this.Stretch);
_minScale = Math.Min(width * 0.1 / size.Width, height * 0.1 / size.Height);
}
else
{
_minScale = 0.1;
} }
} }
@@ -218,7 +234,7 @@ public class ImageViewer: TemplatedControl
{ {
var scale = Scale; var scale = Scale;
scale /= 1.1; scale /= 1.1;
if (scale < 0.1) scale = 0.1; if (scale < _minScale) scale = _minScale;
Scale = scale; Scale = scale;
} }
} }