Android: Cảm ứng đa điểm (MultiTouch)
Cảm ứng đa điểm (MultiTouch) hay Cử chỉ đa chạm xảy ra khi có nhiều ngón tay chạm vào màn hình cùng một lúc. Android cho phép chúng ta phát hiện những cử chỉ này.
Android sẽ tạo ra các sự kiện chạm sau đây bất cứ khi nào có nhiều ngón tay chạm vào màn hình cùng một lúc.
Sự kiện và mô tả |
---|
ACTION_DOWN Được kích thoạt khi ngón tay đầu tiên chạm vào màn hình. |
ACTION_POINTER_DOWN Được kích hoạt khi ngón tay tiếp theo chạm màn hình nhưng khác với vị trí của ngón thứ nhất. |
ACTION_MOVE Được kích hoạt khi di chuyển ngón tay trên màn hình. |
ACTION_POINTER_UP Được kích hoạt khi ngón tay thứ hai được nhấc lên. |
ACTION_UP Được kích hoạt khi ngón tay thứ nhất được nhấc lên. |
Để phát hiện bất kỳ sự kiện nào ở trên thì ta cần ghi đè phương thức onTouchEvent() và kiểm tra các sự kiện này theo cách thủ công như ví dụ sau:
public boolean onTouchEvent(MotionEvent ev){ final int actionPeformed = ev.getAction(); switch(actionPeformed){ case MotionEvent.ACTION_DOWN:{ break; } case MotionEvent.ACTION_MOVE:{ break; } return true; } }
Để có tọa độ của trục X và Y, bạn có thể gọi phương thức getX() và getY(). Cú pháp cụ thể như sau:
final float x = ev.getX(); final float y = ev.getY();
Dưới đây là các phương thức khác ngoài hai phương thức getX() và getY() ở trên:
Phương thức và mô tả |
---|
getAction() Phương thức này trả về loại hành động đang được thực hiện |
getPressure() Phương thức này trả về áp lực hiện tại của sự kiện này cho chỉ mục đầu tiên |
getRawX() Phương thức này trả về tọa độ X thô ban đầu của sự kiện này |
getRawY() Phương thức này trả về tọa độ Y thô ban đầu của sự kiện này |
getSize() Phương thức này trả về kích thước cho chỉ mục con trỏ đầu tiên |
getSource() Phương thức này lấy nguồn của sự kiện |
getXPrecision() Phương thức này trả về độ chính xác của tọa độ X được báo cáo |
getYPrecision() Phương thức này trả về độ chính xác của tọa độ Y được báo cáo |
Ví dụ áp dụng
Nội dung của file MainActivity.java:
import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { float xAxis = 0f; float yAxis = 0f; float lastXAxis = 0f; float lastYAxis = 0f; EditText ed1, ed2, ed3, ed4; TextView tv1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ed1 = (EditText) findViewById(R.id.editText); ed2 = (EditText) findViewById(R.id.editText2); ed3 = (EditText) findViewById(R.id.editText3); ed4 = (EditText) findViewById(R.id.editText4); tv1=(TextView)findViewById(R.id.textView2); tv1.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { final int actionPeformed = event.getAction(); switch(actionPeformed){ case MotionEvent.ACTION_DOWN:{ final float x = event.getX(); final float y = event.getY(); lastXAxis = x; lastYAxis = y; ed1.setText(Float.toString(lastXAxis)); ed2.setText(Float.toString(lastYAxis)); break; } case MotionEvent.ACTION_MOVE:{ final float x = event.getX(); final float y = event.getY(); final float dx = x - lastXAxis; final float dy = y - lastYAxis; xAxis += dx; yAxis += dy; ed3.setText(Float.toString(xAxis)); ed4.setText(Float.toString(yAxis)); break; } } return true; } }); } }
Nội dung của file activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:transitionGroup="true"> <TextView android:text="Multitouch example" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview" android:textSize="35dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tutorials point" android:id="@+id/textView" android:layout_below="@+id/textview" android:layout_centerHorizontal="true" android:textColor="#ff7aff24" android:textSize="35dp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/abc" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" android:theme="@style/Base.TextAppearance.AppCompat" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:layout_below="@+id/imageView" android:layout_alignRight="@+id/textview" android:layout_alignEnd="@+id/textview" android:hint="X-Axis" android:layout_alignLeft="@+id/textview" android:layout_alignStart="@+id/textview" android:textColorHint="#ff69ff0e" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText2" android:layout_below="@+id/editText" android:layout_alignLeft="@+id/editText" android:layout_alignStart="@+id/editText" android:textColorHint="#ff21ff11" android:hint="Y-Axis" android:layout_alignRight="@+id/editText" android:layout_alignEnd="@+id/editText" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText3" android:layout_below="@+id/editText2" android:layout_alignLeft="@+id/editText2" android:layout_alignStart="@+id/editText2" android:hint="Move X" android:textColorHint="#ff33ff20" android:layout_alignRight="@+id/editText2" android:layout_alignEnd="@+id/editText2" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText4" android:layout_below="@+id/editText3" android:layout_alignLeft="@+id/editText3" android:layout_alignStart="@+id/editText3" android:textColorHint="#ff31ff07" android:hint="Move Y" android:layout_alignRight="@+id/editText3" android:layout_alignEnd="@+id/editText3" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Touch here" android:id="@+id/textView2" android:layout_alignParentBottom="true" android:layout_alignLeft="@+id/imageView" android:layout_alignStart="@+id/imageView" android:focusable="true" android:typeface="sans" android:clickable="true" android:textColor="#ff5480ff" android:textSize="35dp" /> </RelativeLayout>