Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

Android multitouch tutorial

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-25-2011, 01:32 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Android multitouch tutorial

Using Agraham's Gestures library we can handle multitouch events.

This is an example of a simple drawing application that supports multitouch:



As you can see in the above screenshot, each finger draws with a different color and it also displays the current id and position of each touch.

In order to support multitouch a Gestures object is used to add a listener to a specific view.
The listener is responsible for raising an event with every movement or change in the multitouch state.

Inside this event we are handling the gesture. Each touch gets a pointer id during a gesture. The pointer id is assigned when a finger touches the screen and is released when the same finger leaves the screen.

I find it useful to maintain a Map with the pointer ids as the keys and the the extra data that you want to assign to each touch as values.
In our example we store the color and the previous position as the value.

Maintaining the map is done with the following code:
Code:
Sub GesturesTouch(View As Object, PointerID As Int, Action As Int, X As Float, Y As FloatAs Boolean
    
Dim p As Point
    
Select Action
        
Case g.ACTION_DOWN, g.ACTION_POINTER_DOWN
            
'New Point is assigned to the new touch
            p.Id = PointerID
            p.Color = 
Colors.RGB(Rnd(0255), Rnd(0255), Rnd(0255))
            TouchMap.Put(PointerID, p)
        
Case g.ACTION_POINTER_UP
            TouchMap.Remove(PointerId)
        
Case g.ACTION_UP
            TouchMap.Clear
    
End Select
  ...
The Point type is declared in Sub Globals:
Code:
Type Point(Id As Int, prevX As Int, prevY As Int, Color As Int)
The result is that TouchMap holds information about the active touches.
Now all that is left to do is to iterate over the TouchMap values and connect the previous coordinates with the new ones and then update the previous coordinates.
The new coordinates are retrieved by calling g.GetX/Y with the pointer id.
Code:
    Dim px, py As Int
For i = 0 To TouchMap.Size - 1
    p = TouchMap.GetValueAt(i)
    px = g.GetX(p.id) 
'Get the current coordinates of this touch
    py = g.GetY(p.id)
    
Dim s As String
    s = p.Id & 
"" & px & " x " & py
    
Canvas.DrawText(s, 10dip20dip + i * RowHeight, Typeface.DEFAULT, TextSize, p.Color, "LEFT")
    
If p.prevX > 0 AND p.prevY > 0 Then
        
Canvas.DrawLine(p.prevX, p.prevY, px, py, p.Color, 5dip)
    
End If
    p.prevX = px
    p.prevY = py
Next
bgd.Invalidate
p.prevX and p.prevY will equal 0 when the touch starts. In this case we do not draw anything.

The gesture sub should return true or otherwise no more events will be raised for this gesture.
Attached Files
File Type: zip MultitouchExample.zip (5.5 KB, 372 views)
Reply With Quote
  #2 (permalink)  
Old 07-25-2011, 06:25 PM
Basic4ppc Veteran
 
Join Date: Jun 2011
Posts: 298
Default

So, were you going to do this anyway or is this your way of trying to get me to use the gesture library?

Nice that its a small working demo (more comments), works on my phone and tablet - wee.
Reply With Quote
  #3 (permalink)  
Old 07-25-2011, 06:33 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Actually it was a coincidence. I believe that the tutorial was posted before your question...
Reply With Quote
  #4 (permalink)  
Old 02-27-2012, 12:30 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

What do you mean? Multitouch only works if you specifically handle it in your application (except of WebView).
Reply With Quote
  #5 (permalink)  
Old 02-27-2012, 01:32 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

You should not use this library if you don't want to handle multitouch.

If it is a different issue then please start a new thread for this question.
Reply With Quote
  #6 (permalink)  
Old 04-14-2012, 09:01 AM
Knows the basics
 
Join Date: Jul 2011
Posts: 65
Default

When i start the MultitouchExample on my emulator or on my lg p920 device i get the message "The application xxx has stopped unexpectedly"
Reply With Quote
  #7 (permalink)  
Old 04-14-2012, 09:14 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Can you check the logs? There should be an error message with a stack trace.
Reply With Quote
  #8 (permalink)  
Old 04-14-2012, 09:27 AM
Knows the basics
 
Join Date: Jul 2011
Posts: 65
Default

Quote:
Originally Posted by Erel View Post
Can you check the logs? There should be an error message with a stack trace.
Thanks for your fast answer. I used an old lib and installed the 1.2 one. now i get no error message, but the screen get black and nothing happens. at the emulator and the real device. When i compile it in debug modus, it works well. i dont understand that
Update:
Release works too
Release (obfuscated) don't work!

Last edited by Nachtfalke75 : 04-14-2012 at 11:42 AM.
Reply With Quote
  #9 (permalink)  
Old 04-14-2012, 02:51 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

The problem is that GesturesTouch gets obfuscated and then the event cannot be raised.

Rename the event (and the SetListener line) to Gestures_Touch and it will work. The obfuscator doesn't obfuscate identifiers with underscore.
Reply With Quote
  #10 (permalink)  
Old 04-14-2012, 07:01 PM
Knows the basics
 
Join Date: Jul 2011
Posts: 65
Default

Quote:
Originally Posted by Erel View Post
The problem is that GesturesTouch gets obfuscated and then the event cannot be raised.

Rename the event (and the SetListener line) to Gestures_Touch and it will work. The obfuscator doesn't obfuscate identifiers with underscore.
Ok, thank you
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Android FTP tutorial Erel Basic4android Getting started & Tutorials 104 Yesterday 05:51 PM
Android Serial tutorial Erel Basic4android Getting started & Tutorials 78 05-20-2012 05:39 AM
Android JSON tutorial Erel Basic4android Getting started & Tutorials 5 05-07-2012 03:33 AM
Android FTP Tutorial klaus German Tutorials 1 02-04-2012 04:01 PM
Android JSON Tutorial klaus German Tutorials 0 02-11-2011 10:31 PM


All times are GMT. The time now is 09:57 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0