我正在尝试使用其他应用功能(例如地图)的点击或捏合在我的wp7应用中为图像启用缩放功能。我见过的大部分帖子都是老的,我想知道是否有更新的方法来实现这个目标?
答案 0 :(得分: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;
}
如果您想要快速双击或重置尺寸,您还可以跟踪原始比例。
答案 1 :(得分: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;
}