基于TCP和多线程实现无线鼠标键盘-GestureDetector
生活随笔
收集整理的這篇文章主要介紹了
基于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" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content" ><Buttonandroid:id="@+id/btn_options"style="?android:attr/buttonStyleSmall"android:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_weight="1"android:text="設置" /><Buttonandroid: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><TextViewandroid:id="@+id/txt_mouse"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginBottom="5dp"android:layout_marginTop="5dp"android:text="鼠標靈敏度:" /><SeekBarandroid:id="@+id/skb_mouse"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:max="400"android:progress="100" /><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content" ><Buttonandroid:id="@+id/btn_left"style="?android:attr/buttonStyleSmall"android:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_weight="1"android:text="左鍵" /><Buttonandroid: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><TextViewandroid:id="@+id/txt_touch"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1" /><Buttonandroid: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{@Overridepublic 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; elsereturn false; }這樣,就可以識別出用戶在手機屏幕上操作鼠標的動作,并且發送給Windows端。
轉載于:https://www.cnblogs.com/mstk/p/3451285.html
總結
以上是生活随笔為你收集整理的基于TCP和多线程实现无线鼠标键盘-GestureDetector的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [个人网站搭建]·Django增加评论功
- 下一篇: 将DataTable 存到一个集合当中