download: http://www.mediafire.com/download/3obheaff1ut7sgl/MultiTouchHandler.zip
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
Button button1, button2 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.rl1);
button1.setOnTouchListener(new RelativeLayout.OnTouchListener() {
public boolean onTouch(View v, MotionEvent m) {
handleTouch(m);
return true;
}
});
button2.setOnTouchListener(new RelativeLayout.OnTouchListener() {
public boolean onTouch(View v, MotionEvent m) {
handleTouch(m);
return true;
}
});
}
void handleTouch(MotionEvent m)
{
int pointerCount = m.getPointerCount();
for (int i = 0; i < pointerCount; i++)
{
int x = (int) m.getX(i);
int y = (int) m.getY(i);
int id = m.getPointerId(i);
int action = m.getActionMasked();
int actionIndex = m.getActionIndex();
String actionString;
switch (action)
{
case MotionEvent.ACTION_DOWN:
actionString = "DOWN";
break;
case MotionEvent.ACTION_UP:
actionString = "UP";
break;
case MotionEvent.ACTION_POINTER_DOWN:
actionString = "PNTR DOWN";
break;
case MotionEvent.ACTION_POINTER_UP:
actionString = "PNTR UP";
break;
case MotionEvent.ACTION_MOVE:
actionString = "MOVE";
break;
default:
actionString = "";
}
String touchStatus = "Action: " + actionString + " Index: " + actionIndex + " ID: " + id + " X: " + x + " Y: " + y+ " pointerCount="+pointerCount;
if (pointerCount == 1)
button1.setText(touchStatus);
else
button2.setText(touchStatus);
}
}
}
No comments:
Post a Comment