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
-
@Idevicegazette (iDevice Gazette)
GSM-to-Skype bridge lets you lose those roaming fees http://bit.ly/lbRJeh #android
12 years 4 weeks ago -
@tommy_banane (tom b.)
RT @AndroidFavorite: #Android New Desktop Android Market Is Live, Adds Several New Features http://zorr0.nl/lFwXNz
12 years 4 weeks ago -
@dwilliams5 (Dennis Williams)
just completed a runtastic run of 3.02 km in 40 min 11 s with #runtastic #Android App: http://tinyurl.com/5tvrpe3
12 years 4 weeks ago -
@S_Pinz (Spinz!)
RT @Androidheadline: Out of box #LG Optimus 3D got Quadrant 2420 score. Thanks @blink_c #io2011 #android http://twitpic.com/4whkdz
12 years 4 weeks ago -
@tayaitapps (Taya IT)
Next Google TV Looks A Lot Like Android http://t.co/dvlTim3 via @alleyinsider #google #apple #android #tv #honeycomb
12 years 4 weeks ago
Poll
Useful resources
Android Development Projects
- App Development
- Telegram bulk messages sender
- Photo Editing Application
- React Native Developer - part time basis
- Locket App Copy
- Courier app $150 only
- Mobile Application Development - IOS and Android (Rewards for sharing brand ads on social media)
- Android app for sms
- Fitness app project for sale online
- io developer