借花献佛 WP7 中图片查看 缩放
大家在WP7 中 展现图片是我想肯定很纠结于如何实现 类似 手机照片查看是 照片能自由缩放 拖动的功能。
在这里我就借 toolkit 的花 给大家share一个小技巧
在 sl toolkit for wp 中 有个东西叫做 gestrue
在这里我就借 toolkit 的花 给大家share一个小技巧
在 sl toolkit for wp 中 有个东西叫做 gestrue
功能我就不描述了 大家自己去看toolkit
我们要用的就是它 .
我们可以在toolkit 里面的把这个页面翻出来看看
cs 代码一大堆 就不贴了
xaml 里面是
1 <Border x:Name="border" Width="300" Height="200" BorderBrush="{StaticResource PhoneForegroundBrush}" BorderThickness="4,16,4,4" Background="{StaticResource PhoneAccentBrush}" RenderTransformOrigin="0.5,0.5" Opacity=".5" CacheMode="BitmapCache"> 2 <Border.RenderTransform> 3 <CompositeTransform x:Name="transform"/> 4 </Border.RenderTransform> 5 <toolkit:GestureService.GestureListener> 6 <toolkit:GestureListener 7 Tap="OnTap" Hold="OnHold" 8 DragStarted="OnDragStarted" DragDelta="OnDragDelta" DragCompleted="OnDragCompleted" 9 Flick="OnFlick" 10 PinchStarted="OnPinchStarted" PinchDelta="OnPinchDelta" PinchCompleted="OnPinchCompleted"/> 11 </toolkit:GestureService.GestureListener> 12 </Border>
我们可以注意到 作用的就是那个 toolkit:GestureListener
我们现在要做的就是 把弄到 img 里面去 ..
代码如下
xaml
1 <Image x:Name="_image" RenderTransformOrigin="0.5,0.5"> 2 <Image.RenderTransform> 3 <CompositeTransform x:Name="transform"/> 4 </Image.RenderTransform> 5 <toolkit:GestureService.GestureListener> 6 <toolkit:GestureListener 7 Tap="OnTap" Hold="OnHold" 8 DragStarted="OnDragStarted" DragDelta="OnDragDelta" DragCompleted="OnDragCompleted" 9 Flick="OnFlick" 10 PinchStarted="OnPinchStarted" PinchDelta="OnPinchDelta" PinchCompleted="OnPinchCompleted"/> 11 </toolkit:GestureService.GestureListener>
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Net; 5 using System.IO; 6 using System.Windows; 7 using System.Windows.Controls; 8 using System.Windows.Documents; 9 using System.Windows.Input; 10 using System.Windows.Media; 11 using System.Windows.Media.Imaging; 12 using System.Windows.Media.Animation; 13 14 using System.Windows.Shapes; 15 using Microsoft.Phone.Controls; 16 using Microsoft.Xna.Framework.Media; 17 18 namespace Demo 19 { 20 public partial class MainPage : PhoneApplicationPage 21 { 22 //SolidColorBrush greenBrush = new SolidColorBrush(Colors.Green); 23 //SolidColorBrush redBrush = new SolidColorBrush(Colors.Red); 24 //SolidColorBrush normalBrush; 25 26 double initialAngle; 27 double initialScale; 28 29 Uri _imageURI; 30 31 BitmapImage _bitMapImage = new BitmapImage(); 32 33 public MainPage() 34 { 35 InitializeComponent(); 36 } 37 38 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 39 { 40 _imageURI = new Uri("http://photo.fanfou.com/n0/02/5y/3p_215854.jpg"); 41 42 43 _bitMapImage.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(bitMapImage_DownloadProgress); 44 _bitMapImage.UriSource = _imageURI; ; 45 _image.Source = _bitMapImage; 46 _progressBar.Visibility = System.Windows.Visibility.Visible; 47 _progressBar.IsIndeterminate = true; 48 49 base.OnNavigatedTo(e); 50 } 51 52 void bitMapImage_DownloadProgress(object sender, DownloadProgressEventArgs e) 53 { 54 if (e.Progress == 100) 55 { 56 57 _progressBar.Visibility = System.Windows.Visibility.Collapsed; 58 _progressBar.IsIndeterminate = false; 59 } 60 //throw new NotImplementedException(); 61 } 62 63 64 private void OnTap(object sender, Microsoft.Phone.Controls.GestureEventArgs e) 65 { 66 transform.TranslateX = transform.TranslateY = 0; 67 } 68 69 private void OnDoubleTap(object sender, Microsoft.Phone.Controls.GestureEventArgs e) 70 { 71 transform.ScaleX = transform.ScaleY = 0; 72 } 73 74 private void OnHold(object sender, Microsoft.Phone.Controls.GestureEventArgs e) 75 { 76 transform.TranslateX = transform.TranslateY = 0; 77 transform.ScaleX = transform.ScaleY = 0; 78 transform.Rotation = 0; 79 } 80 81 private void OnDragStarted(object sender, DragStartedGestureEventArgs e) 82 { 83 //border.Background = greenBrush; 84 } 85 86 private void OnDragDelta(object sender, DragDeltaGestureEventArgs e) 87 { 88 transform.TranslateX += e.HorizontalChange; 89 transform.TranslateY += e.VerticalChange; 90 } 91 92 private void OnDragCompleted(object sender, DragCompletedGestureEventArgs e) 93 { 94 //border.Background = normalBrush; 95 } 96 97 private void OnPinchStarted(object sender, PinchStartedGestureEventArgs e) 98 { 99 //border.Background = redBrush; 100 101 initialAngle = transform.Rotation; 102 initialScale = transform.ScaleX; 103 } 104 105 private void OnPinchDelta(object sender, PinchGestureEventArgs e) 106 { 107 108 transform.Rotation = initialAngle + e.TotalAngleDelta; 109 transform.ScaleX = transform.ScaleY = initialScale * e.DistanceRatio; 110 } 111 112 private void OnPinchCompleted(object sender, PinchGestureEventArgs e) 113 { 114 // _image.Background = normalBrush; 115 } 116 117 private void OnFlick(object sender, FlickGestureEventArgs e) 118 { 119 //flickData.Text = string.Format("{0} Flick: Angle {1} Velocity {2},{3}", 120 // e.Direction, Math.Round(e.Angle), e.HorizontalVelocity, e.VerticalVelocity); 121 } 122 123 private void ApplicationBarIconButton_Click(object sender, EventArgs e) 124 { 125 MemoryStream fileStream = new MemoryStream(); 126 try 127 { 128 MediaLibrary library = new MediaLibrary(); 129 130 string lName = DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg"; 131 132 133 134 WriteableBitmap CaptureImage = new WriteableBitmap(_bitMapImage); 135 136 // 将WriteableBitmap转换为JPEG流编码,并储存到独立存储里. 137 138 139 Extensions.SaveJpeg(CaptureImage, fileStream, CaptureImage.PixelWidth, CaptureImage.PixelHeight, 0, 100); 140 fileStream.Seek(0, SeekOrigin.Begin); 141 fileStream.Seek(0, SeekOrigin.Current); 142 143 //把图片加在WP7 手机的媒体库. 144 Picture pic = library.SavePicture(lName, fileStream); 145 fileStream.Close(); 146 MessageBox.Show("保存成功!", string.Empty, MessageBoxButton.OK); 147 } 148 catch 149 { 150 MessageBox.Show("保存失败", string.Empty, MessageBoxButton.OK); 151 } 152 finally 153 { 154 fileStream.Close(); 155 } 156 157 158 } 159 } 160 }
Demo下载:https://files.cnblogs.com/qq278360339/Demo.rar
转自:F-sea的bolg