页面手势监控(放大,缩小 平移)

首先  Xaml定义一个Image  实现图片的放大缩小(也可针对页面):

<Image x:Name="_image"  RenderTransformOrigin="0.5,0.5">  
 <Image.RenderTransform> 
<CompositeTransform x:Name="transform"/>
 </Image.RenderTransform> 
 <toolkit:GestureService.GestureListener>
<toolkit:GestureListener  Tap="OnTap" Hold="OnHold" 
 DragStarted="OnDragStarted" DragDelta="OnDragDelta" 
DragCompleted="OnDragCompleted" Flick="OnFlick"  
 PinchStarted="OnPinchStarted" PinchDelta="OnPinchDelta" 
PinchCompleted="OnPinchCompleted"/>  
</toolkit:GestureService.GestureListener>
 </Image>

在CS里面就要针对该页面写事件:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework.Media;
namespace Demo
{
    public partial class MainPage : PhoneApplicationPage    
{
        //SolidColorBrush greenBrush = new SolidColorBrush(Colors.Green);        
//SolidColorBrush redBrush = new SolidColorBrush(Colors.Red);        
//SolidColorBrush normalBrush;        
double initialAngle;        
double initialScale;        
Uri _imageURI;        
BitmapImage _bitMapImage = new BitmapImage();        
public MainPage()        
{            
       InitializeComponent();        
}        
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            _imageURI = new Uri("http://photo.fanfou.com/n0/02/5y/3p_215854.jpg");                        
           _bitMapImage.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(bitMapImage_DownloadProgress);
            _bitMapImage.UriSource = _imageURI;
           _image.Source = _bitMapImage;            
_progressBar.Visibility = System.Windows.Visibility.Visible;
            _progressBar.IsIndeterminate = true;                        
base.OnNavigatedTo(e);        }        
void bitMapImage_DownloadProgress(object sender, DownloadProgressEventArgs e)        
{            
if (e.Progress == 100)            
{                               
_progressBar.Visibility = System.Windows.Visibility.Collapsed;
                _progressBar.IsIndeterminate = false;            
}            //throw new NotImplementedException();        
}        private void OnTap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)        
{            transform.TranslateX = transform.TranslateY = 0;        
}        private void OnDoubleTap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)        
{            transform.ScaleX = transform.ScaleY = 0;
        }        private void OnHold(object sender, Microsoft.Phone.Controls.GestureEventArgs e)        
{            transform.TranslateX = transform.TranslateY = 0;            
transform.ScaleX = transform.ScaleY = 0;            
transform.Rotation = 0;        
}        private void OnDragStarted(object sender, DragStartedGestureEventArgs e)        
{            
//border.Background = greenBrush;       
 }      
  private void OnDragDelta(object sender, DragDeltaGestureEventArgs e)   
     {      
      transform.TranslateX += e.HorizontalChange;    
        transform.TranslateY += e.VerticalChange;     
   }        private void OnDragCompleted(object sender, DragCompletedGestureEventArgs e)   
     {         
   //border.Background = normalBrush;    
    }    
    private void OnPinchStarted(object sender, PinchStartedGestureEventArgs e)  
      {         
   //border.Background = redBrush;    
        initialAngle = transform.Rotation;    
        initialScale = transform.ScaleX;   
     }        private void OnPinchDelta(object sender, PinchGestureEventArgs e)   
     {                     
  transform.Rotation = initialAngle + e.TotalAngleDelta;  
          transform.ScaleX = transform.ScaleY = initialScale * e.DistanceRatio;   
     }        private void OnPinchCompleted(object sender, PinchGestureEventArgs e)  
     {            // _image.Background = normalBrush;     
   }        private void OnFlick(object sender, FlickGestureEventArgs e)    
    {        
    //flickData.Text = string.Format("{0} Flick: Angle {1} Velocity {2},{3}",   
         //    e.Direction, Math.Round(e.Angle), e.HorizontalVelocity, e.VerticalVelocity);       
 }        private void ApplicationBarIconButton_Click(object sender, EventArgs e)  
      {   
         MemoryStream fileStream = new MemoryStream();     
       try          
  {              
  MediaLibrary library = new MediaLibrary();  
              string lName = DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";   
             WriteableBitmap CaptureImage = new WriteableBitmap(_bitMapImage);     
           // 将WriteableBitmap转换为JPEG流编码,并储存到独立存储里.     
           Extensions.SaveJpeg(CaptureImage, fileStream, CaptureImage.PixelWidth, CaptureImage.PixelHeight, 0, 100); 
               fileStream.Seek(0, SeekOrigin.Begin);    
            fileStream.Seek(0, SeekOrigin.Current);    
            //把图片加在WP7 手机的媒体库.              
  Picture pic = library.SavePicture(lName, fileStream);    
            fileStream.Close();          
      MessageBox.Show("保存成功!", string.Empty, MessageBoxButton.OK);          
  }       
     catch         
   {           
     MessageBox.Show("保存失败", string.Empty, MessageBoxButton.OK);          
  }          
  finally       
     {      
          fileStream.Close();  
          }    
    }  
  }
}

 

 

转载于:https://www.cnblogs.com/zhibin/archive/2012/06/18/2553291.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值