[英]How to zoom in and zoom out Images in WP7?
我已经制作了一个显示图像的应用程序。现在我想实现放大和缩小功能(通过使用两个指尖),如在本机Windows手机照片查看器应用程序。任何想法如何继续。
提前致谢。
也许最方便的方法是包含Silverlight for Windows Phone Toolkit 。 这包含一个GestureService
,可以帮助捏合和旋转触摸手势。 您可以将它应用于这样的图像: -
<Image Source="someSourceUrl" RenderTransformOrigin="0.5, 0.5" CacheMode="BitmapCache">
<Image.RenderTransform>
<CompositeTransform x:Name="transform" />
</Image.RenderTransform>
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener PinchStarted="OnPinchStarted" PinchDelta="OnPinchDelta" />
</toolkit:GestureService.GestureListener>
</Image>
然后在代码隐藏中: -
private void OnPinchStarted(object sender, PinchStartedGestureEventArgs e)
{
initialAngle = transform.Rotation;
initialScale = transform.ScaleX;
}
private void OnPinchDelta(object sender, PinchGestureEventArgs e)
{
transform.Rotation = initialAngle + e.TotalAngleDelta;
transform.ScaleX = initialScale * e.DistanceRatio;
transform.ScaleY = initialScale * e.DistanceRatio;
}
查看Laurent Bugnion的多点触控样本 - http://multitouch.codeplex.com/
如果您想要支持多点触控的简单图像查看器,我建议您使用WebBrowser
控件来显示图像。
它默认支持多点触摸缩放和平滑滚动。 但您必须将文件从项目文件夹复制到隔离存储。 这就是我的表现:
<Grid x:Name="LayoutRoot" Background="Transparent">
<phone:WebBrowser
Name="MyWebBrowserControl"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0" />
</Grid>
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
// if image file does not exist in isolated storage, copy it to there~!
if (!isf.FileExists(filename))
{
StreamResourceInfo sr = Application.GetResourceStream(new Uri(filename, UriKind.Relative));
using (BinaryReader br = new BinaryReader(sr.Stream))
{
byte[] data = br.ReadBytes((int)sr.Stream.Length);
using (BinaryWriter bw = new BinaryWriter(isf.OpenFile(filename, FileMode.OpenOrCreate)))
{
bw.Write(data);
bw.Close();
}
br.Close();
}
}
Dispatcher.BeginInvoke(() => { MyWebBrowserControl.Navigate(new Uri(filename, UriKind.Relative)); });
※您必须将图像文件的构建操作设置为内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.