I have a draggable image and I'm trying to get it so that if it goes within an X Y region of the screen that an event happens.
So far I can detect when I move a finger through the region but not when it is dragging the image.
The code so far: - this code works for the finger
public boolean onTouchEvent(MotionEvent ev) {
int x = (int) ev.getX();
int y = (int) ev.getY();
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
if (x >= 370 && x <= 500 && y >= 250 && y <= 420) {
Log.i(null, "--- Region 1 initiated ---");
}
What I've tried: - but when I try to adapt it to the image...
public boolean onDrag(View layoutview, DragEvent dragevent) {
int action = dragevent.getAction();
View getid = findViewById(R.id.image);
int x1 = (int) getid.getX();
int y1 = (int) getid.getY();
if (action == MotionEvent.ACTION_MOVE) {
if (x1 >= 370 && x1 <= 500 && y1 >= 250 && y1 <= 420) {
Log.i(null, "--- Region 1 initiated by image---");
}
}
...nothing happens. The logcat doesn't show the message. Does anyone know where I'm going wrong or if there's a way of doing this.
Some illustrations, sometimes my description of the problem is pretty bad:
It took a while but I actually got it working by doing this in onTouch:
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
ImageView j = (ImageView) findViewById(R.id.image);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
_xDelta = (int) (X - j.getTranslationX());
_yDelta = (int) (Y - j.getTranslationY());
break;
case MotionEvent.ACTION_UP:
_xDelta = 100;
_yDelta = 100;
break;
case MotionEvent.ACTION_MOVE:
j.setTranslationX(X - _xDelta);
j.setTranslationY(Y - _yDelta);
if (X >= 370 && X <= 500 && Y >= 250 && Y <= 420) {
Log.i(null, "Region 1 initiated");
// Do whatever...
}
There is no MotionEvent.ACTION_MOVE defined in DragEvent class
OnDragListener interface defined the method
abstract boolean onDrag(View v, DragEvent event)
Action are:
ACTION_DRAG_STARTED
ACTION_DRAG_ENTERED
ACTION_DRAG_LOCATION
ACTION_DRAG_EXITED
ACTION_DROP
ACTION_DRAG_ENDED
Please refer to this page for more details
Related
I'm trying to make a view invisible when it's dragged to a certain Y coordinate on the screen.
This is what I got now:
private final class dragTouchListener implements View.OnTouchListener {
#Override
public boolean onTouch(View v, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
ContentFrameLayout.LayoutParams lParams = (ContentFrameLayout.LayoutParams) v.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
trash_image.setVisibility(View.INVISIBLE);
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
trash_image.setVisibility(View.VISIBLE);
ContentFrameLayout.LayoutParams layoutParams = (ContentFrameLayout.LayoutParams) v.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
v.setLayoutParams(layoutParams);
if(v.getY() > trash_image.getY()){
v.setVisibility(View.INVISIBLE);
}
break;
}
contentView.invalidate();
return true;
}
}
So basically what I've tried here is simply an if statement to check if the Y position of the view has "passed" that of another ImageView placed on the upper part of the screen. This however result in becoming invisible as soon as I touch the view to be dragged, no matter where on the screen it's located. So it's far from accurate, or wrong all together.
As requested in comment, a suggestion (do note I had to change it, since this is for a client program, so I didnt test it again):
First, discover SCREEN_DENSITY
private static float SCREEN_DENSITY = getResources().getDisplayMetrics().density;
The onTouch function:
public boolean onTouch(final View v, MotionEvent event) {
case MotionEvent.ACTION_DOWN:
mainFinger = new Point(
(int) (event.getRawX() / SCREEN_DENSITY),
(int) (event.getRawY() / SCREEN_DENSITY)
);
break;
case MotionEvent.ACTION_MOVE:
movFrame(
(FrameLayout) v,
(int) (event.getRawX() / SCREEN_DENSITY),
(int) (event.getRawY() / SCREEN_DENSITY)
);
}
movFrame function:
private void movFrame(FrameLayout v, int movX, int movY) {
MyObject p = (MyObject ) v.getTag();
int[] x, y, h, w;
x = p.x;
y = p.y;
h = p.h;
w = p.w;
x = p.x + movX - mainFinger.x;
mainFinger.x = movX;
if (x + w > p.parent.w) {
x = p.parent.w - w;
}
if (x < 0) {
x = 0;
}
y = p.y + movY - mainFinger.y;
mainFinger.y = movY;
if (y + h > p.parent.h) {
y = p.parent.h - h;
}
if (y < 0) {
y = 0;
}
p.updateMyObject(x, y, h, w);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
(int) ((p.w * SCREEN_DENSITY) + 0.5),
(int) ((p.h * SCREEN_DENSITY) + 0.5)
);
params.leftMargin = (int) ((p.x * SCREEN_DENSITY) + 0.5);
params.topMargin = (int) ((p.y * SCREEN_DENSITY) + 0.5);
v.setLayoutParams(params);
}
I am showing camera preview on top using system alert. And I am trying to make it draggable. Initially I am drawing it on top left conrner. But on dragging it, it just comes to the center of the screen and doesn't move after that.
Here is how I am adding relative layout :
RelativeLayout.LayoutParams sf_lp = new RelativeLayout.LayoutParams(xlen,ylen);
relativeLayout.addView(surfaceView,sf_lp);
WindowManager.LayoutParams wLayoutParams = new WindowManager.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,0,0,WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT);
wLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;
windowManager.addView(relativeLayout,wLayoutParams);
Here is the code for OnTouch :
public boolean onTouch(View v, MotionEvent event) {
int X = (int) event.getRawX();
int Y = (int) event.getRawY();
int xpos = relativeLayout.getLeft();
int ypos = relativeLayout.getTop();
if(X >= xpos && X <= xpos+relativeLayout.getWidth() && Y >= ypos && Y <= ypos+relativeLayout.getHeight())
{
Toast.makeText(this,"X="+X+", "+"Y="+Y+"-Touched inside",Toast.LENGTH_SHORT).show();
switch(event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN :
WindowManager.LayoutParams wlp = (WindowManager.LayoutParams) v.getLayoutParams();
dx = X - wlp.x;
dy = Y - wlp.y;
Log.v("onTouch", "Action Down:X=" + X + ", Y=" + Y + " ,dx=" + dx + " ,dy=" + dy);
break;
case MotionEvent.ACTION_UP : Log.v("onTouch","Action Up:X="+X+", Y="+Y+" ,dx="+dx+" ,dy="+dy);
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE :
//windowManager.removeView(v);
Log.v("ACTION_MOVE","X="+ (X-dx) +"Y="+ (Y-dy));
//((TextView) v.findViewWithTag("tv")).setText("X="+ (X-dx) +"Y="+ (Y-dy));
WindowManager.LayoutParams wlp1 = new WindowManager.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT);
wlp1.x = X-dx;
wlp1.y = Y-dy;
windowManager.updateViewLayout(v,wlp1);
//windowManager.addView(v,wlp1);
//v.animate().x(X-dx).y(Y-dy).setDuration(0).start();
Log.v("onTouch", "Action Move:X=" + X + ", Y=" + Y + " ,dx=" + dx + " ,dy=" + dy);
break;
}
}
else
{
Toast.makeText(this,"X="+X+", "+"Y="+Y+"-Touched Outside",Toast.LENGTH_SHORT).show();
}
return false;
}
As you can see in Action Move I have already tried removing the view and the adding it again with new layout parameters but it does the same thing. I can drag a button in an activity with this same code but not this. What am I doing wrong?
I had tried the same using below code and using surfaceview directly rather than relative layout
preview.setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = params.x;
initialY = params.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_UP:
return true;
case MotionEvent.ACTION_MOVE:
params.x = initialX + (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(preview, params);
return true;
}
return false;
}
});
Finally I found what was causing it. The problem was with the 'if' condition where I was trying to determine if user has touched inside the view or outside. It wasn't updating the location of relative layout after dragging.
int xpos = relativeLayout.getLeft();
int ypos = relativeLayout.getTop();
if(X >= xpos && X <= xpos+relativeLayout.getWidth() && Y >= ypos && Y <= ypos+relativeLayout.getHeight())
{
}
I commented the if condition and its working as expected. But I am not sure what was problem with 'getLeft()' and 'getTop()'. I also tried 'getLocationInWindow()' but it didn't update the relative layout's location in screen too. If anyone finds the problem please share. And thanks for your help.
Here is the code for my onTouchEvent(MotionEvent event):
#Override
public boolean onTouchEvent(MotionEvent event){
int action = event.getAction();
int x = Math.round(event.getX());
int y = Math.round(event.getY());
//play is an imageView
int viewLeft = play.getLeft();
int viewRight = play.getRight();
int viewTop = play.getTop();
int viewBottom = play.getBottom();
boolean onPoint = false;
switch (action) {
case MotionEvent.ACTION_DOWN:
//Checks if the touched area falls within the imageView play
if ((x >= viewLeft && x <= viewRight) && (y >= viewTop && y <= viewBottom)) {
onPoint = true;
play.setImageResource(R.drawable.playyellow);
}
case MotionEvent.ACTION_UP:
//if the finger is lifed on the imageView, the intent is called
play.setImageResource(R.drawable.play1);
if (onPoint) {
if ((x >= viewLeft && x <= viewRight) && (y >= viewTop && y <= viewBottom)) {
Intent i = new Intent(this, InGame.class);
startActivity(i);
}
}
}
return true;
}
The problem here is that ACTION_UP is always called, regardless of whether or not I actually lift my finger off the screen. I ran it in debugging mode while placing a breakpoint on case MotionEvent.ACTION_UP, and when I pressed down (and didn't release), it got called. Can someone explain why this happens?
I analyzed your code and I think it is just because you did not insert break in between Action_Down and Action_Up cases.
try the following code.....
switch (action) {
case MotionEvent.ACTION_DOWN:
//Checks if the touched area falls within the imageView play
if ((x >= viewLeft && x <= viewRight) && (y >= viewTop && y <= viewBottom)) {
onPoint = true;
play.setImageResource(R.drawable.playyellow);
}
break;
case MotionEvent.ACTION_UP:
//if the finger is lifed on the imageView, the intent is called
play.setImageResource(R.drawable.play1);
if (onPoint) {
if ((x >= viewLeft && x <= viewRight) && (y >= viewTop && y <= viewBottom)) {
Intent i = new Intent(this, InGame.class);
startActivity(i);
}
}
}
return true;
I hope it will help you...
I'm doing a simple match-three game, similar to Bejeweled and I just want to move sprite objects by touching the sprite object and then move it one step in four directions like left, right, up and down. I do this by comparing the X and Y values on Down with the X and Y values on Move. It's working but it's far from perfect! It's so easy to get a wrong value if the movement isn't straight. My questions is: is there a way to improve this and make it better?
I have also looked at gesture, but this seems very complicated to use with a surfaceview that I have.
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.i("test","Down");
touchActionDownX = (int)event.getX();
touchActionDownY = (int)event.getY();
touchActionMoveStatus = true;
gameLoop.touchX = (int)event.getX();
gameLoop.touchY = (int)event.getY();
gameLoop.touchActionDown = true;
break;
case MotionEvent.ACTION_POINTER_UP:
touchActionMoveStatus = true;
break;
case MotionEvent.ACTION_MOVE:
//Log.i("test","Move");
gameLoop.touchActionMove = true;
if(touchActionMoveStatus) {
touchActionMoveX = (int)event.getX();
touchActionMoveY = (int)event.getY();
if(touchActionMoveX < touchActionDownX)
Log.i("test","Move Left");
else if(touchActionMoveX > touchActionDownX)
Log.i("test","Move Right");
else if(touchActionMoveY < touchActionDownY)
Log.i("test","Move Up");
else if(touchActionMoveY > touchActionDownY)
Log.i("test","Move Down");
touchActionMoveStatus = false; // Will be set to true when pointer is up
}
break;
}
// return false;
return true; // This gets the coordinates all the time
}
Try something like this:
#Override
public boolean onTouch(View v, MotionEvent event) {
//You may have to play with the value and make it density dependant.
int threshold = 10;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.i("test","Down");
touchActionDownX = (int)event.getX();
touchActionDownY = (int)event.getY();
touchActionMoveStatus = true;
gameLoop.touchX = (int)event.getX();
gameLoop.touchY = (int)event.getY();
gameLoop.touchActionDown = true;
break;
case MotionEvent.ACTION_POINTER_UP:
touchActionMoveStatus = false;
break;
case MotionEvent.ACTION_MOVE:
//Log.i("test","Move");
gameLoop.touchActionMove = true;
if(touchActionMoveStatus) {
touchActionMoveX = (int)event.getX();
touchActionMoveY = (int)event.getY();
if(touchActionMoveX < (touchActionDownX - threshold) && (touchActionMoveY > (touchActionDownY - threshold)) && (touchActionMoveY (touchActionDownY + threshold))){
Log.i("test","Move Left");//If the move left was greater than the threshold and not greater than the threshold up or down
touchActionMoveStatus = false;
}
else if(touchActionMoveX > (touchActionDownX + threshold) && (touchActionMoveY > (touchActionDownY - threshold)) && (touchActionMoveY < (touchActionDownY + threshold))){
Log.i("test","Move Right");//If the move right was greater than the threshold and not greater than the threshold up or
touchActionMoveStatus = false;
}
else if(touchActionMoveY < (touchActionDownY - threshold) && (touchActionMoveX > (touchActionDownX - threshold)) && (touchActionMoveX < (touchActionDownX + threshold))){
Log.i("test","Move Up");//If the move up was greater than the threshold and not greater than the threshold left or right
touchActionMoveStatus = false;
}
else if(touchActionMoveY > (touchActionDownY + threshold) && (touchActionMoveX > (touchActionDownX - threshold)) && (touchActionMoveX < (touchActionDownX + threshold))){
Log.i("test","Move Down");//If the move down was greater than the threshold and not greater than the threshold left or right
touchActionMoveStatus = false;
}
}
break;
}
// return false;
return true; // This gets the coordinates all the time
}
Or use a ratio:
#Override
public boolean onTouch(View v, MotionEvent event) {
//You may have to play with the value.
//A value of two means you require the user to move twice as
//far in the direction they intend to move than any perpendicular direction.
float threshold = 2.0;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.i("test","Down");
touchActionDownX = (int)event.getX();
touchActionDownY = (int)event.getY();
touchActionMoveStatus = true;
gameLoop.touchX = (int)event.getX();
gameLoop.touchY = (int)event.getY();
gameLoop.touchActionDown = true;
break;
case MotionEvent.ACTION_POINTER_UP:
touchActionMoveStatus = true;
break;
case MotionEvent.ACTION_MOVE:
//Log.i("test","Move");
gameLoop.touchActionMove = true;
if(touchActionMoveStatus) {
touchActionMoveX = (int)event.getX();
touchActionMoveY = (int)event.getY();
// I haven't tested this so you may have a few typos to correct.
float ratioLeftRight = Math.abs(touchActionMoveX - touchActionDownX)/Math.abs(touchActionMoveY - touchActionDownY)
float ratioUpDown = Math.abs(touchActionMoveY - touchActionDownY)/Math.abs(touchActionMoveX - touchActionDownX)
if(touchActionMoveX < touchActionDownX && ratioLeftRight > threshold){
Log.i("test","Move Left");
touchActionMoveStatus = false;
}
else if(touchActionMoveX > touchActionDownX && ratioLeftRight > threshold){
Log.i("test","Move Right");
touchActionMoveStatus = false;
}
else if(touchActionMoveY < touchActionDownY && ratioUpDown > threshold){
Log.i("test","Move Up");
touchActionMoveStatus = false;
}
else if(touchActionMoveY > touchActionDownY && ratioUpDown > threshold){
Log.i("test","Move Down");
touchActionMoveStatus = false;
}
}
break;
}
// return false;
return true; // This gets the coordinates all the time
}
I would choose the dimension with the LARGEST movement and completely ignore the other, for example if the move is x=10 and y=8 then only use the x dimension (i.e. left/right) and vice versa.
Also as noted by Larry McKenzie, using a threshold to ignore smaller movements is a good idea to prevent registering accidental movements that the user did not intend. Tweak the threshold value to someting that feels natural.
Here is some code using your example (only the ACTION_MOVE case):
case MotionEvent.ACTION_MOVE:
//Log.i("test","Move");
gameLoop.touchActionMove = true;
if(touchActionMoveStatus) {
touchActionMoveX = (int)event.getX();
touchActionMoveY = (int)event.getY();
// setup a threshold (below which no movement would occur)
int threshold = 5; /* tweak this as needed */
// first calculate the "delta" movement amounts
int xMove = touchActionMoveX - touchActionDownX;
int yMove = touchActionMoveY - touchActionDownY;
// now find the largest of the two (note that if they
// are equal, x is assumed largest)
if ( Math.abs( xMove ) >= Math.abs( yMove ) ) { /* X-Axis */
if ( xMove >= threshold )
Log.i("test","Move Right");
else if ( xMove <= -threshold )
Log.i("test","Move Left");
}
else { /* Y-Axis */
if ( yMove >= threshold )
Log.i("test","Move Down");
else if ( yMove <= -threshold )
Log.i("test","Move Up");
}
touchActionMoveStatus = false; // Will be set to true when pointer is up
}
}
break;
NOTE: As mentioned in some of the other answers, because multiple events with very small values can occur, it might be best to accumulate (i.e. sum up) the movements UNTIL the threshold is reached - you can use members for this that reset in ACTION_DOWN. Once the threshold is reached (in either dimension) THEN you can perform the checks for which direction.
Alternative Approach
Another way to go about it would be to detect the largest movement in the FIRST ACTION_MOVE event, and then lock all further movements to that dimension. For this you would need to add various state members - these would need to be updated in each state.
Here is a rough example (with only the state tracking):
// members
private boolean axisLock = false; /* Track When Lock is Required */
private boolean axisX = true; /* Axis to Lock (true) for X, (false) for Y */
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// set this state so that ACTION_MOVE knows a lock is required
axisLock = true;
break;
case MotionEvent.ACTION_UP:
// clear the state in case no move was made
axisLock = false;
case MotionEvent.ACTION_MOVE:
// now lock the axis if this is the first move event
if ( axisLock ) {
// this will set whether the locked axis is X (true) or Y (false)
axisX = event.getX() >= event.getY();
// reset the state (to keep the axis locked)
axisLock = false;
}
// at this point you only need to consider the movement for the locked axis
if ( axisX ) {
int movement = (int)event.getX(); /* Get Movement for Locked Axis */
// check for your movement conditions here
}
else {
int movement = (int)event.getY(); /* Get Movement for Locked Axis */
// check for your movement conditions here
}
break;
}
return true;
}
You could add many optimizations to this code, for now it just illustrates the basic idea.
larry had the right idea, i just want to put in a lil fix,
//put this in the wraping class
private static int THRESHOLD = 10;
private static int initX;
private static int initY;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initX = (int)event.getX();
initY = (int)event.getY();
break;
case MotionEvent.ACTION_POINTER_UP:
//you can add in some kind of "move back" animation for the item
break;
case MotionEvent.ACTION_MOVE:
if(((int)event.getY - initY) > THRESHOLD){
//move down
break;
}
if(((int)event.getY - initY) > -THRESHOLD){
//move up
break;
}
if(((int)event.getX - initX) > THRESHOLD){
//move right
break;
}
if(((int)event.getX - initX) < -THRESHOLD){
//move left
break;
}
break;
}
}
i didn't test this code, only free write it, but i hope you get my idea :)
Hi im making a split screen game and i cannot figure out how to get the controls down. Right now they work fine if only one person is touching the screen but once multiple do the first person to touch is the only one who can control. So i need to implement multitouch. I cant figure out how to do it so once one finger is down controlling the object on the top half of the screen another finger can be put down controlling the bottom half of the screen. How should i do this?
EDIT:
I changed it, it still doesnt work though could somebody tell me why
My attempt that doesnt work:
public boolean onTouch(View v, MotionEvent event){
//needs multitouch
if(checkInGame()){
if(event.getY() < this.getMeasuredHeight() / 2){
pointerId1 = event.getPointerId(event.getActionIndex());
}else{
pointerId2 = event.getPointerId(event.getActionIndex());
}
switch(event.getAction() & MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_MOVE:{
if(event.getPointerId(event.getActionIndex()) == pointerId1){
Log.d("Touch","Move1");
int pointerIndex = event.getActionIndex();
int moveX = 0;
int moveY = 0;
moveX = (int) event.getX(pointerIndex) - sX;
moveY = (int) event.getY(pointerIndex) - sY;
ship1.setLoc(moveX, moveY);
sX = sX + moveX;
sY = sY + moveY;
}else if(event.getPointerId(event.getActionIndex()) == pointerId2){
int pointerIndex = event.getActionIndex();
Log.d("Touch","Move2");
int moveX = 0;
int moveY = 0;
moveX = (int) event.getX(pointerIndex) - sX2;
moveY = (int) event.getY(pointerIndex) - sY2;
ship2.setLoc(moveX, moveY);
sX2 = sX2 + moveX;
sY2 = sY2 + moveY;
}
return true;
}
case MotionEvent.ACTION_DOWN: {
if(event.getPointerId(event.getActionIndex()) == pointerId1){
Log.d("Touch","Top1");
int pointerIndex = event.getActionIndex();
sX = (int) event.getX(pointerIndex);
sY = (int) event.getY(pointerIndex);
}else if(event.getPointerId(event.getActionIndex()) == pointerId2){
int pointerIndex = event.getActionIndex();
Log.d("Touch","Bottom1");
sX2 = (int) event.getX(pointerIndex);
sY2 = (int) event.getY(pointerIndex);
}
return true;
}
case MotionEvent.ACTION_POINTER_DOWN:{
if(event.getPointerId(event.getActionIndex()) == pointerId1){
Log.d("Touch","Top2");
int pointerIndex = event.getActionIndex();
sX = (int) event.getX(pointerIndex);
sY = (int) event.getY(pointerIndex);
}else if(event.getPointerId(event.getActionIndex()) == pointerId2){
int pointerIndex = event.getActionIndex();
Log.d("Touch","Bottom2");
sX2 = (int) event.getX(pointerIndex);
sY2 = (int) event.getY(pointerIndex);
}
return true;
}
}
You probably want to track each pointer by its ID. On pointer down, check the ID so you know that pointer with ID 1 is controlling object 1. Give this Android blog post a read, it may help: http://android-developers.blogspot.co.uk/2010/06/making-sense-of-multitouch.html