25 Sep

Deteksi Multitouch di Android

Kita perlu melakukan deteksi multitouch untuk keperluan khusus, misalnya saja kita ingin membuat alat musik digital di Android. Berikut ulasan javaclopedia tentang Multitouch di Android:
1. Kita Buat kelas Extends View. Kita sebut saja MultitouchView

public class MultitouchView extends View {
        public MultitouchView(Context context) {
                super(context);
        } 
}

2. Di dalamnya perlu kita masukkan 2 konstruktur sbb:

        public MultitouchView(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
 
        }
 
        public MultitouchView(Context context, AttributeSet attrs) {
                super(context, attrs);
 
        }

3. Masuk ke main.xml (res/layout/main/xml) untuk mengatur layoutnya:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
<com.helloandroid.multitouchexample.MultitouchView
        android:id="@+id/touchView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
 
</LinearLayout>

4. Buat activity sebagai main class:

public class Main extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
        }
}

5. Tambahkan method berikut di kelas MultitouchView:

@Override
        public boolean onTouchEvent(MotionEvent event) {
                super.onTouchEvent(event);
 
               
                int action = event.getAction() & MotionEvent.ACTION_MASK;
       
                switch (action) {
                case MotionEvent.ACTION_DOWN: {
                        Log.d("MultitouchExample","Action Down");
                        break;
                }
                case MotionEvent.ACTION_MOVE: {
                        Log.d("MultitouchExample","Action Move");
                        break;
                }
 
                case MotionEvent.ACTION_POINTER_DOWN: {
                        Log.d("MultitouchExample","Pointer Down");
                break;
                }
                case MotionEvent.ACTION_POINTER_UP: {
                        Log.d("MultitouchExample","Pointer up");
 
                        break;
                }
                case MotionEvent.ACTION_UP: {
                        Log.d("Multitouch", "Action up");
               
                        break;
                }
                }
 
                return true;
        }

Berikut event-event yang dapat ditangani:
ACTION_DOWN dipanggil ketika jari user pertama menyentuh screen.
ACTION_MOVE dipanggil ketika user menggeser jarinya di screen.
ACTION_POINTER_DOWN dipanggil ketika jari user kedua kalinya menyentuk screen (multitouch).

Leave a Reply

Your email address will not be published. Required fields are marked *