Để hiện thực chức năng chuột không dây, cần nhận diện các thao tác vuốt trên màn hình điện thoại. Lớp GestureDetector trong Android sẽ được sử dụng cho mục đích này.
Đầu tiên, file bố cục activity_main.xml được định nghĩa như sau:
<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="Cài đặt" />
<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="Kết nối" />
</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="Độ nhạy chuột:" />
<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="Nút trái" />
<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="Nút phải" />
</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="Bàn phím" />
</LinearLayout>
Vùng trống ở giữa là nơi người dùng thao tác điều khiển chuột. Để nhận diện cử chỉ, ta tạo lớp Mouse_GestureListener kế thừa từ 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();
// Kiểm tra khoảng cách di chuyển có đủ lớn không
double distance = Math.sqrt(Math.pow(MainActivity.dis_x, 2) + Math.pow(MainActivity.dis_y, 2));
if (distance > 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;
}
Phương thức onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) được gọi khi người dùng vuốt trên màn hình. e1 là điểm chạm đầu tiên, e2 là điểm chạm cuối cùng. Hiệu số tọa độ x và y giữa hai điểm cho biết khoảng cách di chuyển theo trục X và Y. Khoảng cách này sau đó được nhân với hệ số độ nhạy chuột và gửi đến thread gửi dữ liệu để chuyển tiếp tới máy Windows.
Trong lớp MainActivity, khai báo biến thành viên:
GestureDetector gd;
Trong phương thức onCreate(Bundle savedInstanceState), thêm dòng lệnh khởi tạo:
gd = new GestureDetector(this, new Mouse_GestureListener());
Cuối cùng, ghi đè phương thức onTouchEvent trong MainActivity:
public boolean onTouchEvent(MotionEvent event) {
if (gd.onTouchEvent(event))
return true;
else
return false;
}
Như vậy, hệ thống có thể nhận diện các thao tác di chuyển chuột của người dùng trên màn hình điện thoại và gửi dữ liệu tới máy tính Windows để xử lý.