我正在尝试使用像其他应用程序功能(即地图)一样的点击或捏合在我的 wp7 应用程序中启用图像的缩放功能。我看到的与此相关的大多数帖子都已经过时了,我想知道是否有更新的方法来实现这一目标?
问问题
662 次
2 回答
4
不知道是否有更好的东西,但这是我正在做的事情:
<Image Name="displayImage" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Source="{Binding photo_link}" RenderTransformOrigin="0.5, 0.5" CacheMode="BitmapCache">
<Image.RenderTransform>
<CompositeTransform x:Name="transform" />
</Image.RenderTransform>
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener PinchDelta="OnPinchDelta" PinchStarted="OnPinchStarted" />
</toolkit:GestureService.GestureListener>
</Image>
private void OnPinchStarted(object sender, PinchStartedGestureEventArgs e)
{
var image = sender as System.Windows.Controls.Image;
if (image == null) return;
var transform = image.RenderTransform as CompositeTransform;
if (transform == null) return;
initialScale = transform.ScaleX;
}
private void OnPinchDelta(object sender, PinchGestureEventArgs e)
{
var image = sender as System.Windows.Controls.Image;
if (image == null) return;
var transform = image.RenderTransform as CompositeTransform;
if (transform == null) return;
transform.ScaleX = initialScale * e.DistanceRatio;
transform.ScaleY = initialScale * e.DistanceRatio;
}
如果您想快速双击或重置大小,您还可以跟踪原始比例。
于 2012-04-25T18:12:50.343 回答
3
您需要使用比例变换。如果您有兴趣,我可以稍后再写一篇博文。对于捏缩放拖动轻弹等,您需要使用工具包中的手势服务
private void GestureListener_DragDelta(object sender, DragDeltaGestureEventArgs e)
{
scaleTrans.CenterX = (scaleTrans.CenterX - e.HorizontalChange);
scaleTrans.CenterY = (scaleTrans.CenterY - e.VerticalChange);
if (scaleTrans.CenterX < 0)
scaleTrans.CenterX = 0;
else if (scaleTrans.CenterX > (imgViewer.Height * scaleTrans.ScaleX))
scaleTrans.CenterX = imgViewer.Height * scaleTrans.ScaleX;
if(scaleTrans.CenterY < 0)
scaleTrans.CenterY = 0;
else if (scaleTrans.CenterY > (imgViewer.Height * scaleTrans.ScaleY))
scaleTrans.CenterY = imgViewer.Height * scaleTrans.ScaleY;
}
于 2012-04-25T18:12:02.497 回答