基于TCP和多线程实现无线鼠标键盘-GestureDetector
为了实现无线鼠标,需要识别出用户在手机屏幕上的滑动动作,这就需要用到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的更多相关文章
- 基于TCP和多线程实现无线鼠标键盘-Socket(1)
把手机作为移动鼠标.键盘使用非常方便,本文将实现这一功能.该应用分为两部分:Windows服务端和Android客户端. 本文源代码的下载地址:http://download.csdn.net/det ...
- 基于TCP和多线程实现无线鼠标键盘-Robot
Windows端收到Android端传递来的键盘或鼠标操作信息以后,需要根据这些信息操作鼠标或键盘,这就需要用到java.awt.Robot类,该类用于控制鼠标或键盘. 在Java主窗体中定义: pu ...
- 基于TCP和多线程实现无线鼠标键盘-InputMethodManager
为了实现无线键盘的功能,使用了InputMethodManager弹出软键盘. // 弹出软键盘 public void pop_keyboard(){ imm = (InputMethodManag ...
- 基于TCP和多线程实现无线鼠标键盘-Socket(2)
在(1)中,实现了Windows服务端与Android客户端的连接,本节将实现在Windows服务端与Android客户端之间传递数据. Android客户端的发送线程SendThread.java代 ...
- 基于tcp和多线程的多人聊天室-C语言
之前在学习关于网络tcp和多线程的编程,学了知识以后不用一下总绝对心虚,于是就编写了一个基于tcp和多线程的多人聊天室. 具体的实现过程: 服务器端:绑定socket对象->设置监听数-> ...
- Java Socket实现基于TCP和UDP多线程通信
一.通过Socket实现TCP编程 1.1 TCP编程 TCP协议是面向连接,可靠的,有序的,以字节流的方式发送数据.基于TCP协议实现网络通信的类有客户端的Socket类和服务器端的ServerSo ...
- JAVA基础知识之网络编程——-基于TCP通信的简单聊天室
下面将基于TCP协议用JAVA写一个非常简单的聊天室程序, 聊天室具有以下功能, 在服务器端,可以接受客户端注册(用户名),可以显示注册成功的账户 在客户端,可以注册一个账号,并用这个账号发送信息 发 ...
- (1)基于tcp协议的编程模型 (2)tcp协议和udp协议的比较 (3)基于udp协议的编程模型 (4)反射机制
1.基于tcp协议的编程模型(重中之重)1.1 编程模型服务器: (1)创建ServerSocket类型的对象,并提供端口号: (2)等待客户端的连接请求,调用accept()方法: (3)使用输入输 ...
- Java Web 基础(一) 基于TCP的Socket网络编程
一.Socket简单介绍 Socket通信作为Java网络通讯的基础内容,集中了异常.I/O流模式等众多知识点.学习Socket通信,既能够了解真正的网络通讯原理,也能够增强对I/O流模式的理解. 1 ...
随机推荐
- asp.net web api CORS
using System; using System.Web.Http.Filters; public class AllowCrossSiteJsonAttribute : ActionFilter ...
- Java-面向对象基础练习
1.编写一个Java应用程序,该应用程序包括2个类:Print类和主类E.Print 类里有一个方法output()功能是输出100 ~ 999之间的所有水仙花数(各位数字的 立方和等于这个三位数本身 ...
- 基础才是重中之重~stream和byte[]的概念与转化
回到目录 多看几篇 之所以写这篇文章完全是因为最近在研究FastDFS这个分布式的文件存储系统,当然这不是我第一次研究它了,就像我们去看一本书,我们不会只看一篇,而是一次次,一篇篇,每看一次会有新的收 ...
- [读书笔记]C#学习笔记一: .Net Framwork
前言: 一次偶然的机会 在园子里看到@Learning hard 出版的一本书: <<C#学习笔记>>, 然后买来 一直到现在读完, 感觉很不错, 适合入门, 书中内容是从C ...
- Atitit cnchar simp best list 汉字简化方案 最简化汉字256个
Atitit cnchar simp best list 汉字简化方案 最简化汉字256个 1.1. 最简化发音1 1.2. 根据笔画密度,删除了密度高的字..1 1.3. 使用同发音的英文字母等代 ...
- iOS-SDWebImage
我之前写过一篇博客,介绍缓存处理的三种方式,其中最难,最麻烦,最占内存资源的还是图片缓存,最近做的项目有大量的图片处理,还是采用了SDWebImage来处理,但是发现之前封装好的代码报错了.研究发现, ...
- html5 浏览器端数据库
为什么使用浏览器端数据库:随着浏览器的处理能力不断增强,越来越多的网站开始考虑,将大量数据储存在客户端,这样可以减少用户等待从服务器获取数据的时间. 一.localStorage — 本地存储 可 ...
- vuejs - the component is a fragment instance
vuejs - the component is a fragment instance http://vuejs.org/guide/components.html#Fragment-Instanc ...
- 【原创】NIO框架入门(四):Android与MINA2、Netty4的跨平台UDP双向通信实战
概述 本文演示的是一个Android客户端程序,通过UDP协议与两个典型的NIO框架服务端,实现跨平台双向通信的完整Demo. 当前由于NIO框架的流行,使得开发大并发.高性能的互联网服务端成为可能. ...
- WP、Win10开发或者WPF开发时绘制自定义窗体~例如:一个手机
WP and Win10 效果:(数字是参考值,和UI无关) <Page x:Class="_05.AllControls._BorderUsePage" xmlns=&qu ...