Implement the custom view to detect multi-touch, by overriding onTouchEvent().
package com.exercise.AndroidMultiTouch;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class MultiToucnView extends View {
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
final int MAX_NUMBER_OF_POINT = 10;
float[] x = new float[MAX_NUMBER_OF_POINT];
float[] y = new float[MAX_NUMBER_OF_POINT];
boolean[] touching = new boolean[MAX_NUMBER_OF_POINT];
public MultiToucnView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MultiToucnView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MultiToucnView(Context context) {
super(context);
init();
}
void init() {
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1);
paint.setColor(Color.WHITE);
}
@Override
protected void onDraw(Canvas canvas) {
for(int i = 0; i < MAX_NUMBER_OF_POINT; i++){
if(touching[i]){
canvas.drawCircle(x[i], y[i], 50f, paint);
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
MeasureSpec.getSize(heightMeasureSpec));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = (event.getAction() & MotionEvent.ACTION_MASK);
int pointCount = event.getPointerCount();
for (int i = 0; i < pointCount; i++) {
int id = event.getPointerId(i);
//Ignore pointer higher than our max.
if(id < MAX_NUMBER_OF_POINT){
x[id] = (int)event.getX(i);
y[id] = (int)event.getY(i);
if((action == MotionEvent.ACTION_DOWN)
||(action == MotionEvent.ACTION_POINTER_DOWN)
||(action == MotionEvent.ACTION_MOVE)){
touching[id] = true;
}else{
touching[id] = false;
}
}
}
invalidate();
return true;
}
}
Modify main.xml to include the custom view, com.exercise.AndroidMultiTouch.MultiToucnView.
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<com.exercise.AndroidMultiTouch.MultiToucnView
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Download the files.
6 comments:
Hi how can i get this code to work as an activity, launched from another activity using an Intent ?
hello Ben Reddicliffe,
depends on your code, you can modify your view to implement it.
Great stuff! The only problem here though is whenever a pointer goes up all the other pointers will become false for a short time. Do you have any solutions how to get rid of that?
This is a great post.
The examples in NVidia Development Kit implement similar approach.
To fix the false positives whenever a pointer goes up, I've created additional boolean right after "touching" to distinguish false positives.
When a pointer goes down, if both bools are false the pointer is registered as a new touch. In case the "if((action == MotionEvent.ACTION_DOWN)
||(action == MotionEvent.ACTION_POINTER_DOWN)
||(action == MotionEvent.ACTION_MOVE))" is positive, both bools are set to "true".
When a pointer goes up, only the false positive bool is set to false. If there is a single pointer, both bools are set to false.
After the loop but before "invalidate(); return true;", a new loop goes through all IDs with event.findPointerIndex(). If the ID does not exist and the false_positive is false, then the pointer is invalidated.
Here is the actual touch implementation in my project:
final int MAX_NUMBER_OF_POINT = 20;
float[] x = new float[MAX_NUMBER_OF_POINT];
float[] y = new float[MAX_NUMBER_OF_POINT];
boolean[] touching = new boolean[MAX_NUMBER_OF_POINT];
boolean[] false_positive = new boolean[MAX_NUMBER_OF_POINT];
int lastPointercounter;
public boolean onTouchEvent(MotionEvent event)
{
int action = (event.getAction() & MotionEvent.ACTION_MASK);
int pointCount = event.getPointerCount();
for (int i = 0; i < pointCount; i++)
{
int id = event.getPointerId(i);
//Ignore pointer higher than our max.
if(id < MAX_NUMBER_OF_POINT)
{
x[id] = (int)event.getX(i);
y[id] = (int)event.getY(i);
if((action == MotionEvent.ACTION_DOWN)
||(action == MotionEvent.ACTION_POINTER_DOWN)
||(action == MotionEvent.ACTION_MOVE))
{
if(touching[id]!=true&&false_positive[id]!=true)
{
ToucheBegan2( x[id], y[id], tap_count, id );
}
else
{
ToucheMoved2( x[id], y[id], tap_count , id);
}
touching[id] = true;
false_positive[id]=true;
}
else
{
false_positive[id]=false;
if(pointCount==1)
{
touching[id]=false;
ToucheEnded2( tap_count, id);
}
}
}
}
for( int i=0; i < MAX_NUMBER_OF_POINT; i++)
{
if(event.findPointerIndex(i)==-1&&false_positive[i]!=true)
{
touching[i]=false;
ToucheEnded2( tap_count, i);
}
}
invalidate();
return true;
}
PS: I will add comments later.
This is great, but in my device only detect 4 pointers, i downloaded the project and tested and this ocurrs... any idea??
Thanks
hello Ollie SP,
What is your device?
Post a Comment