我正在编写一个具有缩放和平移能力的WPF应用程序,但我想要实现的是能够“自动”缩放和平移(通过单击按钮)。
我将所有方法都定义为缩放和平移,但我无法告诉应用程序平移所需的X / Y坐标。
基本上,我知道我希望控件以所需的缩放级别为中心(比如放大6倍),但是平移目标点不是控件的中心点,因为在缩放之后,它已经缩放。
有没有人知道计算所需的X / Y位置的方法,同时考虑到缩放?我只是缩放所需的目标点吗?它似乎对我不起作用......
非常感谢
编辑 - 已完成 -
现在我所拥有的工作正常:)
<Canvas x:Name="LayoutRoot" Background="{DynamicResource WindowBackground}" Width="1024" Height="768">
<Canvas x:Name="ProductCanvas" Width="1024" Height="768">
<Canvas.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform />
</TransformGroup>
</Canvas.RenderTransform>
<Rectangle x:Name="r1" Fill="White" Stroke="Black" Width="180" Height="103.5" Canvas.Left="131.5" Canvas.Top="121.5" MouseDown="r1_MouseDown"/>
<Rectangle x:Name="r2" Fill="#FF942222" Stroke="Black" Width="180" Height="103.5" Canvas.Left="617.5" Canvas.Top="121.5" MouseDown="r2_MouseDown"/>
<Rectangle x:Name="r3" Fill="#FF2B1E9F" Stroke="Black" Width="180" Height="103.5" Canvas.Left="131.5" Canvas.Top="408" MouseDown="r3_MouseDown"/>
<Rectangle x:Name="r4" Fill="#FF1F6E1D" Stroke="Black" Width="180" Height="103.5" Canvas.Left="617.5" Canvas.Top="408" MouseDown="r4_MouseDown"/>
</Canvas>
</Canvas>
---- C#----
private void r1_MouseDown(object sender, MouseButtonEventArgs e1)
{
Rect bounds = r1.TransformToAncestor(ProductCanvas).TransformBounds(new Rect(0, 0, r1.ActualWidth, r1.ActualHeight));
ZoomInAndPan(5, new Point(bounds.TopLeft.X + (bounds.Width / 2), bounds.TopLeft.Y + (bounds.Height / 2)));
}
private void r2_MouseDown(object sender, MouseButtonEventArgs e1)
{
Rect bounds = r2.TransformToAncestor(ProductCanvas).TransformBounds(new Rect(0, 0, r2.ActualWidth, r2.ActualHeight));
ZoomInAndPan(5, new Point(bounds.TopLeft.X + (bounds.Width / 2), bounds.TopLeft.Y + (bounds.Height / 2)));
}
private void r3_MouseDown(object sender, MouseButtonEventArgs e1)
{
Rect bounds = r3.TransformToAncestor(ProductCanvas).TransformBounds(new Rect(0, 0, r3.ActualWidth, r3.ActualHeight));
ZoomInAndPan(5, new Point(bounds.TopLeft.X + (bounds.Width / 2), bounds.TopLeft.Y + (bounds.Height / 2)));
}
private void r4_MouseDown(object sender, MouseButtonEventArgs e1)
{
Rect bounds = r4.TransformToAncestor(ProductCanvas).TransformBounds(new Rect(0, 0, r4.ActualWidth, r4.ActualHeight));
ZoomInAndPan(5, new Point(bounds.TopLeft.X + (bounds.Width/2), bounds.TopLeft.Y + (bounds.Height/2)));
}
public void ZoomInAndPan(double zoomTo, Point translateTarget)
{
var group = (ProductCanvas.RenderTransform as TransformGroup);
var zoomTransform = group.Children[0] as ScaleTransform;
var translateTransform = group.Children[3] as TranslateTransform;
Point center = new Point(512, 384);
destinationPoint.X *= newScale;
destinationPoint.Y *= newScale;
var deltaX = center.X - (translateTarget.X);
var deltaY = center.Y - (translateTarget.Y);
translateTransform.BeginAnimation(TranslateTransform.XProperty, CreateZoomAnimation(deltaX));
translateTransform.BeginAnimation(TranslateTransform.YProperty, CreateZoomAnimation(deltaY));
zoomTransform.BeginAnimation(ScaleTransform.ScaleXProperty, CreateZoomAnimation(zoomTo));
zoomTransform.BeginAnimation(ScaleTransform.ScaleYProperty, CreateZoomAnimation(zoomTo));
}
private DoubleAnimation CreateZoomAnimation(double toValue)
{
var da = new DoubleAnimation(toValue, new Duration(TimeSpan.FromMilliseconds(700)))
{
AccelerationRatio = 0.1,
DecelerationRatio = 0.9
};
return da;
}
答案 0 :(得分:5)
你在谈论转型 - 平底锅和规模。
您可以通过几种不同的方式执行此操作,但由于您使用的是WPF,您是否有理由不能使用RenderTransforms?
var pointClicked = (where user clicked)
var myWindow = (whatever your window is);
myWindow.RenderTransform = new TransformGroup();
var pan = new TranslateTransform(pointClicked.X, pointClicked.Y);
var scale = new ScaleTransform(6.0,6.0);
myWindow.RenderTransform.Children.Add(pan);
myWindow.RenderTransform.Children.Add(scale);
如果你不想走这条路线,你需要“手动”进行2D转换:但首先进行平移,然后进行比例缩放。转型通常不是共同的;如果你以不同的顺序进行,你会得到错误的结果。
答案 1 :(得分:1)
最初您的视口位于(0,0),图像和视口的大小均为X×Y.您希望按照某个放大系数m缩放大小,以便您的图像大小为m X by m Y,但您的视口(您正在显示的部分)仍然是尺寸为X×Y的矩形,位于图像上的(0,0)处。所以你需要移动视口。
如果您的图像现在是m X by m Y,则可以通过将每个中心点除以2来找到中点。然后,您可以减去视口大小的一半以获得左上角。像(m X / 2 - X / 2,m Y / 2 - Y / 2)。