为了实现无线鼠标,需要识别出用户在手机屏幕上的滑动动作,这就需要用到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. C# WebBrowser 获得选中部分的html源码

    Winform程序 2.0的. 需要引用Microsoft.mshtml. private void Form1_Load(object sender, EventArgs e) { webBrows ...

  2. iOS-数据持久化-偏好设置

    一.简单介绍 很多iOS应用都支持偏好设置,比如保存用户名.密码.字体大小等设置,iOS提供了一套标准的解决方案来为应用加入偏好设置功能 每个应用都有个NSUserDefaults实例,通过它来存取偏 ...

  3. 客户端向服务端传送特殊字符解决方法(检测到有潜在危险的 Request.Form 值)

    当客户端向服务端传输特殊字符时报错,错误信息如下图:

  4. vue for 绑定事件

    vue for 绑定事件 <div id="pro_list" v-for="item in pro_list"> <div class=&q ...

  5. Java基础-接口.编写2个接口:InterfaceA和InterfaceB;在接口InterfaceA中有个方法void printCapitalLetter();在接口InterfaceB中有个方法void printLowercaseLetter();然 后写一个类Print实现接口InterfaceA和InterfaceB,要求 方法 实现输出大写英文字母表的功能,printLowerca

    #34.编写2个接口:InterfaceA和InterfaceB:在接口InterfaceA中有个方法void printCapitalLetter():在接口InterfaceB中有个方法void ...

  6. 用javascript动态创建并提交表单form,表格table

    <script> //helper function to create the formfunction getNewSubmitForm(){ var submitForm = doc ...

  7. 编写Shader时的一些性能考虑

    编写shader时的一些建议:1.只计算需要计算的东西:2.通常,需要渲染的像素比顶点数多,而顶点数又比物体数多很多.所以如果可以,尽量将运算从PS移到VS,或直接通过script来设置某些固定值:3 ...

  8. 邻接表无向图(二)之 C++详解

    本章是通过C++实现邻接表无向图. 目录 1. 邻接表无向图的介绍 2. 邻接表无向图的代码说明 3. 邻接表无向图的完整源码 转载请注明出处:http://www.cnblogs.com/skywa ...

  9. Java多线程系列--“JUC线程池”01之 线程池架构

    概要 前面分别介绍了"Java多线程基础"."JUC原子类"和"JUC锁".本章介绍JUC的最后一部分的内容——线程池.内容包括:线程池架构 ...

  10. Java多线程系列--“JUC线程池”05之 线程池原理(四)

    概要 本章介绍线程池的拒绝策略.内容包括:拒绝策略介绍拒绝策略对比和示例 转载请注明出处:http://www.cnblogs.com/skywang12345/p/3512947.html 拒绝策略 ...