Multitouch and gesture detection part 1
In this little tutorial I’m going to show you how to detect multitouch event on an Activity screen.
First of all let’s create a class next to the Main activity that called MultitouchView. It extends View:
It has only one consturctor that you must implement. You have to add those two another constructors, otherwise the touch handling will not work:
- super(context, attrs, defStyle);
- }
- super(context, attrs);
- }
Now let’s edit your main.xml. I’m going to add a new xml element that’s called
com.helloandroid.multitouchex
After this your main xml will look like this:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.c
om/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>
We no need to modify the main activity of the application:
- public class Main extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- }
With the setContentView method you can set the main xml as your activity content layout.
Now go back to MultitouchView class and implement the method that catch the motionevents, called onTouchEvent.
- @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;
- }
You can get the user action from the MotionEvent object. There are a lot of constant actions that you can use to implement your own algorythm with them.
ACTION_DOWN is called when the user’s first finger reaches the screen.
ACTION_MOVE is called when the user move his finger on screen.
ACTION_POINTER_DOWN is called when the user touches the screen with a second, third non-primary-finger. In the next tutorial we are going to use this action to handle the multi touch input.
When the user’s non primary finger leave the screen, ACTION_POINTER_UP is called.
At the time when the last finger leave the screen the ACTION_UP is called.
In my next tutorial I’m going to show you an example how to use System.currentTimeMillis() to detect swipe gesture with multitouch.
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@StephanieNich10 (Stephanie Nichols)#android I laughed so hard at ochocinco 's avi. Hahahaha http://t.co/MPuhhi3m
26 weeks 1 day ago -
@CarlaAtkins8 (Carla Atkins)#android Omg! This is actually f'n interesting http://t.co/JodMOehr
26 weeks 1 day ago -
@MarianMcleod12 (Marian Mcleod)#android Precisely what song is? http://t.co/YmJXU0rB
26 weeks 1 day ago -
@JoBeach15 (Jo Beach)#android haha this made me laugh, i love ted:-) http://t.co/gtcWQ79C
26 weeks 1 day ago -
@aochart3 (青ちゃ)Start playing Paradise Island on Android http://t.co/DEID0Ao5 #Android #Androidgames #Gameinsight http://t.co/e1bifSeL
26 weeks 1 day ago
Poll
Useful resources
Android Development Projects
- iphone ipad app - repost by dreamaginat
- Anroid Expert needed for making 2 version of apk from existing code and features by milliondollarmil
- Build me a Very Complex iPhone/iPad App - repost by fallinddfallindd
- program for an APP by majorpain515
- Built Website and mobile app by ADICTUA1
- Browser plugin and possible mobile app development by Nesset
- Android Mobile App for location tracing with image capture - repost by urTrack
- Android Mobile App for location tracing with image capture by urTrack
- Create app for iOS and Android by realifysystems
- Druapl pro wanted with api for apps experience - start today - hire quick by tampacoder



