你好,游客 登录 注册 搜索
背景:
阅读新闻

Windows Phone 7的多点触摸技术

[日期:2012-04-26] 来源:oschina  作者:虫虫 [字体:大 中 小]
我刚刚发布了一个App.(具体可以看看这里)。这个应用可以让用户使用几个手指同时在屏幕上触摸并作出反应。好了,我们一起来看看这是如何做到的。

因为这个演示是要用到 TouchFrameEventhandler 事件的,所以你必须添加以下.net命名空间。
 
1 System.Windows.Input.Touch

然后建立一个类,叫做 DrawingPoints,接着创建一个包含大量此对象的列表集合,如下图代码所示。
 
01 class DrawingPoints
02 {
03     public int id { get; set; }
04     public Point currentPoint { get; set; }
05     public Point oldPoint { get; set; }
06     public bool drawing { get; set; }
07     public SolidColorBrush currentColor { get; set; }
08     public double drawSize { get; set; }
09 }
10    
11 List<DrawingPoints> drawingPoints = new List<DrawingPoints>();


接着在TouchEvent事件触发的处理方法中添加以下代码
 
01 void Touch_FrameReported(object sender, TouchFrameEventArgs e)
02 {
03     //This holds a collection of touches on the screen. So for each finger that touches the canvas one touch get added.
04     TouchPointCollection tCollection = e.GetTouchPoints(DrawingCanvas);
05    
06     if (tCollection != null)
07     {
08         //iterate through the collection of touches
09         foreach (var point in tCollection)
10         {
11             //sets up a drawing point
12             DrawingPoints drawPoint;
13    
14             //if the touch action is the finger leaving the screen, remove the drawing point from the list of drawpoints
15             if (point.Action == TouchAction.Up)
16             {
17                 var oldPoint = drawingPoints.Single(x => x.id == point.TouchDevice.Id);
18    
19                 if (oldPoint != null)
20                 {
21                     drawingPoints.Remove(oldPoint);
22                 }
23             }
24    
25             //if the touch action is down, add the point to the drawingpoint
26             if (point.Action == TouchAction.Down)
27             {
28                 //gets a list of colors to set which color is set as a drawing point
29                 var colors = App.GetDrawingColors();
30    
31                 var color = colors.Single(c => c.ColorID == settings.DrawColor);
32                 var size = settings.DrawSize;
33    
34                 //create a drawpoint and assing it Touchevent details
35                 drawPoint = new DrawingPoints();
36    
37                 drawPoint.id = point.TouchDevice.Id; //sets the drawpoint an ID so it knows which finger is touching the screen
38                 drawPoint.currentPoint = point.Position; //gets the position
39                 drawPoint.oldPoint = drawPoint.currentPoint;
40    
41                 drawPoint.currentColor = color.ColorBrush;
42                 drawPoint.drawSize = size;
43    
44                 drawingPoints.Add(drawPoint);
45             }
46    
47             //if the touch action is move, move the point and draw a path from the old point to the new point
48             if (point.Action == TouchAction.Move)
49             {
50                 drawPoint = drawingPoints.Single(x => x.id == point.TouchDevice.Id);
51    
52                 drawPoint.currentPoint = point.Position;
53    
54                 draw(drawPoint); //calls the draw action method
55    
56                 drawPoint.oldPoint = drawPoint.currentPoint;
57             }
58         }
59     }
60    
61 }


这样每当手指触碰到屏幕或者屏幕的某个可见元素时,TouchCollection在代码中就添加一个TouchPoint。
然后对于TouchCollection里的每一个TouchPoint,都去检测这个触摸点对应的行为,但其实说白了,这些行为总共就3种,Up -- 手指离开屏幕,Down -- 手指触摸到屏幕以及 Move -- 手指在屏幕上移动。
从上面的代码看到:当触摸行为是Up时,drawingPoints集合去除这个点然后停止绘制。当行为为Down时,drawingPoints集合添加这个点并做好移动行为的准备,所以当你手指移动的时候,就会在手指当前位置与之前的点坐标绘制出一条直线。

好了,现在你将要在程序进入到此页面后立即订阅这个多点触摸事件处理方法。以及程序退出该页面时取消订阅这个事件的处理方法。
 
01 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
02 {
03     base.OnNavigatedTo(e);
04    
05     Touch.FrameReported += new TouchFrameEventHandler(Touch_FrameReported); //Adds the event to the page
06 }
07    
08 protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
09 {
10     base.OnNavigatedFrom(e);
11    
12     Touch.FrameReported -= new TouchFrameEventHandler(Touch_FrameReported); //removes the event from the page
13 }


综上所述,这就是我刚发布的那个应用的精髓所在。如果你有更多的问题请email我吧(不过我只会英语哦:D)。
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款