在wp7中缩放和滚动图像

时间:2012-05-29 04:53:51

标签: windows-phone-7

提前致谢..

在我的WP7应用程序中,我使用以下代码来缩放图像。

<Image Name="imgThumbnail" Width="480" Height="740" VerticalAlignment="Center" HorizontalAlignment="Center" 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 个答案:

答案 0 :(得分:3)

尝试使用Laurent Bugnion的这个库做pinch&amp; amp;放大图像:https://multitouch.codeplex.com/ - 它处理“真正”捏合和放大所需的计算。缩放行为。

代码看起来像:

<Image CacheMode="BitmapCache" Name="imgThumbnail"  Margin="0,0,0,80" Source="test.jpg">
        <i:Interaction.Behaviors>
            <behaviors:MultiTouchBehavior IsDebugModeActive="False"
                                   AreFingersVisible="True"
                                   IsMockActive="True"                                       
                                   MaximumScale="3"
                                   MinimumScale="0.8"
                                   IsScaleEnabled="True"
                                   IsRotateEnabled="False"
                                   IsTranslateXEnabled="True"
                                   IsTranslateYEnabled="True"/>
        </i:Interaction.Behaviors>
    </Image>

如果您想尝试自己实施,我建议您阅读Francesco De Vittori的这篇文章(http://www.frenk.com/2011/03/windows-phone-7-correct-pinch-zoom-in-silverlight/)。