为了实现无线鼠标,需要识别出用户在手机屏幕上的滑动动作,这就需要用到GestureDetector类。

首先是activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="20dp"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <Button
android:id="@+id/btn_options"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="设置" /> <Button
android:id="@+id/btn_connect"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="连接" /> </LinearLayout> <TextView
android:id="@+id/txt_mouse"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:text="鼠标灵敏度:" /> <SeekBar
android:id="@+id/skb_mouse"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:max="400"
android:progress="100" /> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" > <Button
android:id="@+id/btn_left"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="左键" /> <Button
android:id="@+id/btn_right"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="右键" /> </LinearLayout> <TextView
android:id="@+id/txt_touch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" /> <Button
android:id="@+id/btn_keyboard"
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="键盘" /> </LinearLayout>

运行后的效果:

中间的空白区即是用户操作鼠标的区域,为了识别用户的动作,定义Mouse_GestureListener类,该类继承自GestureDetector.SimpleOnGestureListener:

class Mouse_GestureListener extends GestureDetector.SimpleOnGestureListener{

	@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) { MainActivity.dis_x = e2.getX()-e1.getX();
MainActivity.dis_y = e2.getY()-e1.getY();
// 移动距离是否足够
if ((float)Math.pow((Math.pow(MainActivity.dis_x,2)+Math.pow(MainActivity.dis_y,2)),0.5)
              >MainActivity.dis_t){ MainActivity.dis_x *= MainActivity.move_times;
MainActivity.dis_y *= MainActivity.move_times;
MainActivity.send_thread.set_str(MainActivity.df2.format(MainActivity.dis_x)+
                  "/"+MainActivity.df2.format(MainActivity.dis_y)); } return true; }

onFling(MotionEvent e1, MotionEvent e2, float velocityX,  float velocityY)即手指在屏幕上滑动时的事件,e1是第一个点,e2是第二个点,计算这两个点的x坐标和y坐标之差,就是这次滑动在x轴和y轴上移动的距离,并且将计算出来的距离乘以鼠标灵敏度,交给发送线程发送给Windows端。

在MainActivity类中定义:

GestureDetector gd;

在onCreate(Bundle savedInstanceState)方法中加上一句:

gd = new GestureDetector(this, new Mouse_GestureListener());

还要在MainActivity类中定义方法

	 public boolean onTouchEvent(MotionEvent event) { 

		  if (gd.onTouchEvent(event))
return true;
else
return false; }

这样,就可以识别出用户在手机屏幕上操作鼠标的动作,并且发送给Windows端。

基于TCP和多线程实现无线鼠标键盘-GestureDetector的更多相关文章

  1. 基于TCP和多线程实现无线鼠标键盘-Socket(1)

    把手机作为移动鼠标.键盘使用非常方便,本文将实现这一功能.该应用分为两部分:Windows服务端和Android客户端. 本文源代码的下载地址:http://download.csdn.net/det ...

  2. 基于TCP和多线程实现无线鼠标键盘-Robot

    Windows端收到Android端传递来的键盘或鼠标操作信息以后,需要根据这些信息操作鼠标或键盘,这就需要用到java.awt.Robot类,该类用于控制鼠标或键盘. 在Java主窗体中定义: pu ...

  3. 基于TCP和多线程实现无线鼠标键盘-InputMethodManager

    为了实现无线键盘的功能,使用了InputMethodManager弹出软键盘. // 弹出软键盘 public void pop_keyboard(){ imm = (InputMethodManag ...

  4. 基于TCP和多线程实现无线鼠标键盘-Socket(2)

    在(1)中,实现了Windows服务端与Android客户端的连接,本节将实现在Windows服务端与Android客户端之间传递数据. Android客户端的发送线程SendThread.java代 ...

  5. 基于tcp和多线程的多人聊天室-C语言

    之前在学习关于网络tcp和多线程的编程,学了知识以后不用一下总绝对心虚,于是就编写了一个基于tcp和多线程的多人聊天室. 具体的实现过程: 服务器端:绑定socket对象->设置监听数-> ...

  6. Java Socket实现基于TCP和UDP多线程通信

    一.通过Socket实现TCP编程 1.1 TCP编程 TCP协议是面向连接,可靠的,有序的,以字节流的方式发送数据.基于TCP协议实现网络通信的类有客户端的Socket类和服务器端的ServerSo ...

  7. JAVA基础知识之网络编程——-基于TCP通信的简单聊天室

    下面将基于TCP协议用JAVA写一个非常简单的聊天室程序, 聊天室具有以下功能, 在服务器端,可以接受客户端注册(用户名),可以显示注册成功的账户 在客户端,可以注册一个账号,并用这个账号发送信息 发 ...

  8. (1)基于tcp协议的编程模型 (2)tcp协议和udp协议的比较 (3)基于udp协议的编程模型 (4)反射机制

    1.基于tcp协议的编程模型(重中之重)1.1 编程模型服务器: (1)创建ServerSocket类型的对象,并提供端口号: (2)等待客户端的连接请求,调用accept()方法: (3)使用输入输 ...

  9. Java Web 基础(一) 基于TCP的Socket网络编程

    一.Socket简单介绍 Socket通信作为Java网络通讯的基础内容,集中了异常.I/O流模式等众多知识点.学习Socket通信,既能够了解真正的网络通讯原理,也能够增强对I/O流模式的理解. 1 ...

随机推荐

  1. iOS 获取键盘相关信息

    一,在需要的地方添加监听 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardWil ...

  2. 前端chrome浏览器调试总结

    引言 "工欲善其事,必先利其器" 恩,这句话我觉得说的特别有道理,举个例子来说吧,厉害的化妆师都有一套非常专业的刷子,散粉刷负责定妆,眼影刷负责打眼影,各司其职,有了专业的工具才能 ...

  3. Redis中统计各种数据大小的方法

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/125.html?1455853369如果 MySQL 数据库比较大的话,我 ...

  4. fir.im Weekly - 人人都需要的 IT 技能图谱

    AlphaGo 与李世石的人机世纪大战落下帷幕,不禁让人思考<失控> 中说道的 "机器正在生物化,而生物正在工程化 ".作为人类,在未来能否保全最后的智力骄傲成为一个疑 ...

  5. WPF入门教程系列十三——依赖属性(三)

    四. 只读依赖属性 在以前在对于非WPF的功能来说,对于类的属性的封装中,经常会对那些希望暴露给外界只读操作的字段封装成只读属性,同样在WPF中也提供了只读属性的概念,如一些 WPF控件的依赖属性是只 ...

  6. Unity5.x在WP8.1中无法使用Reflection API的解决方法

    下班前随便写点,虽然花了不少时间但是最终得到的解决方法还是比较简单的. 第一种方法:使用WinRTLegacy.dll中的类.这个dll在生成的WP project中是自带的无需在unity工程中添加 ...

  7. 部署到IIS报错:HTTP错误500.19,错误代码0x800700d

    title=部署到IIS报错:HTTP错误500.19,错误代码0x800700d.   用vs直接运行网站没问题,部署到IIS就报错,由此可知应该是IIS中不支持网站相关配置. 查找发现在web.c ...

  8. Html与CSS快速入门02-HTML基础应用

    这部分是html细节知识的学习. 快速入门系列--HTML-01简介 快速入门系列--HTML-02基础元素 快速入门系列--HTML-03高级元素和布局 快速入门系列--HTML-04进阶概念 示例 ...

  9. Hadoop官方文档翻译——YARN Architecture(2.7.3)

    The fundamental idea of YARN is to split up the functionalities of resource management and job sched ...

  10. PHP学习总结(一)

    对最近学习PHP做个简单的总结吧 书籍:<PHP和MySQL Web开发> 环境/工具:wamp/Editplus&Chrome 时间:8月2日-8月7日 内容: 以前把前3章学了 ...