Remove the TouchEvent of Button - android

I want to remove the TouchEvent of Button when the User moves out of the Button Area
I have detected the user movement by:
case MotionEvent.ACTION_DOWN:
rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
break;
case MotionEvent.ACTION_MOVE:
if(!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())){
logIt("You are OutSide...");
}
break;
Can Anyone Suggest how to do that...
ANy answer Appreciated...Thks

reWrite code like this :-
case MotionEvent.ACTION_DOWN:
rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
break;
case MotionEvent.ACTION_UP:
if(!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())){
logIt("You are OutSide...");
}
break;

try this...
case MotionEvent.ACTION_DOWN:
rect = new Rect(v.getLeft(), v.getTop(), v.getRight(),v.getBottom());
break;
case MotionEvent.ACTION_MOVE:
if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop()+ (int) event.getY())) {
Log.i(TAG, "You are outside");
return false;
}
break;
and this complete code will give you some idea...
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
rect = new Rect(v.getLeft(), v.getTop(), v.getRight(),v.getBottom());
break;
case MotionEvent.ACTION_MOVE:
if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop()+ (int) event.getY())) {
Log.i(TAG, "You are outside");
return false;
}
break;
}
return true;
}

Simpliest way :
onTouchEvent(MotionEvent event){
...
case MotionEvent.ACTION_MOVE:
event.setAction(MotionEvent.ACTION_UP);
return super.onTouchEvent(event);
...
}
In That case you will reject all next events related on your button.

Related

OnTouchEvent , my scene go back if i touch some place in scene

my question can be pretty strange, but I can not understand why my scene is moving back, if I touch the screen elsewhere, please help me figure it out.
https://youtu.be/oKdwqn0HC0Y - in this video record my problem,
To better understand the problem
#Override
public boolean onTouchEvent(MotionEvent event) {
final int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
return true;
}
switch (event.getPointerCount()) {
case 3:
Log.e("Event", "PointerCount = " +event.getPointerCount());
return mScaleDetector.onTouchEvent(event);
case 2:
Log.e("Event", "PointerCount = " +event.getPointerCount());
return doRotationEvent(event);
case 1:
Log.e("Event", "PointerCount = " +event.getPointerCount());
return doMoveEvent(event);
}
return true;
}
private boolean doMoveEvent(MotionEvent event)
{
final int action = event.getAction();
Log.e("doMoveEvent", "action = " +action);
switch (action) {
case MotionEvent.ACTION_DOWN: {
Log.e("doMoveEvent", "action_down");
final float x = event.getX();
Log.e("doMoveEvent", " x = " +x);
final float y = event.getY();
Log.e("doMoveEvent", " y = " +y);
// Remember where we started
mLastTouchX = x;
mLastTouchY = y;
break;
}
case MotionEvent.ACTION_MOVE: {
final float x = event.getX();
final float y = event.getY();
// Calculate the distance moved
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
Log.e("doMoveEvent", " DX = " +dx);
Log.e("doMoveEvent", " DY = " +dy);
renderer.angleX += dy;
renderer.angleY += dx;
mLastTouchX = x;
mLastTouchY = y;
// Invalidate to request a redraw
break;
}
case MotionEvent.ACTION_UP: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_CANCEL: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
}
return true;
}
I solved my problem this way
#Override
public boolean onTouchEvent(MotionEvent event) {
final int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
final float x = event.getX(); //(NEW)
final float y = event.getY(); //(NEW)
mLastTouchX = x; //(NEW)
mLastTouchY = y; //(NEW)
return true;
}
switch (event.getPointerCount()) {
case 3:
Log.e("Event", "PointerCount = " +event.getPointerCount());
return mScaleDetector.onTouchEvent(event);
case 2:
Log.e("Event", "PointerCount = " +event.getPointerCount());
return doRotationEvent(event);
case 1:
Log.e("Event", "PointerCount = " +event.getPointerCount());
return doMoveEvent(event);
}
return true;
}

How to map the touch x, y to my canvas

I have a task to build image editor and to be able to draw on my image
attached is my example
the problem now that when I try to draw the drawing line is shifted, and also when I try to draw outside of my image, it draw also, so how can I solve this problem
my code is as following :
float touchX = event.getX();
float touchY = event.getY();
Log.i("AMIRA" , "X: " + touchX + " Y: " +touchY);
//respond to down, move and up events
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
drawPath.moveTo(touchX, touchY);
break;
case MotionEvent.ACTION_MOVE:
canvasBitmap = ((FastBitmapDrawable) getDrawable()).getBitmap();
mutableBitmap = canvasBitmap.copy(Bitmap.Config.ARGB_8888, true);
drawCanvas = new Canvas(mutableBitmap);
float x1= touchX - drawCanvas.getClipBounds().left;
float y1= touchY - drawCanvas.getClipBounds().top;
//drawPath.lineTo(touchX, touchY);
drawPath.lineTo(x1, y1);
drawCanvas.drawPath(drawPath, drawPaint);
setImageBitmap(mutableBitmap);
break;
case MotionEvent.ACTION_UP:
canvasBitmap = ((FastBitmapDrawable) getDrawable()).getBitmap();
mutableBitmap = canvasBitmap.copy(Bitmap.Config.ARGB_8888, true);
drawCanvas = new Canvas(mutableBitmap);
float x2= touchX - drawCanvas.getClipBounds().left;
float y2= touchY - drawCanvas.getClipBounds().top;
//drawPath.lineTo(touchX, touchY);
drawPath.lineTo(x2, y2);
drawCanvas.drawPath(drawPath, drawPaint);
drawPath.reset();
setImageBitmap(mutableBitmap);
break;
default:
return false;
}
invalidate();
return true;

Android multitouch: no ACTION_POINTER_DOWN and UP events

Here is my code:
public boolean onTouch(View v, MotionEvent event) {
switch(event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
Log.d("getButtonCode", "catch ACTION_DOWN");
break;
case MotionEvent.ACTION_POINTER_DOWN:
Log.d("getButtonCode", "catch ACTION_POINTER_DOWN");
break;
case MotionEvent.ACTION_POINTER_UP:
Log.d("getButtonCode", "catch ACTION_POINTER_UP");
break;
case MotionEvent.ACTION_UP:
Log.d("getButtonCode", "catch ACTION_UP");
case MotionEvent.ACTION_CANCEL:
Log.d("getButtonCode", "catch ACTION_CANCEL");
case MotionEvent.ACTION_MOVE:
break;
default:
break;
}
return true;
}
and I never catch ACTION_POINTER_DOWN and ACTION_POINTER_UP. If I remove breakfrom ACTION_DOWN, I catch ACTION_POINTER_DOWN always after ACTION_DOWN, even if it is first pointer. I think it should be so: first pointer catch only ACTION_DOWN, every next pointer only ACTION_POINTER_DOWN, if non-primary pointer has gone up, I should catch ACTION_POINTER_UP.
But it doesn't work. What is wrong in my code?
PS: I saw other similar questions, but no answer helped me.
Instead of using a switch statement, I flowed through a series of if statements. This let me test against event.getAction() or event.getActionMasked() and it allowed multiple possible actions through at once.
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
startingX = (int) event.getX();
startingY = (int) event.getY();
}
if(event.getAction() == MotionEvent.ACTION_MOVE) {
int scaleX = 1;
int scaleY = 1;
if(touchStateScale == true){
scaleX = (int) (event.getX(0) - event.getX(1));
scaleY = (int) (event.getY(0) - event.getY(1));
}
int endingX = (int) event.getX();
int endingY = (int) event.getY();
int newX = endingX-startingX;
int newY = endingY-startingY;
((ImageView) v).setImageBitmap(drawNewImageScaled(baseImage, newImageResourceId, newX, newY, scaleX, scaleY));
}
if(event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN){
Log.i(TAG, "second pointer detected");
touchStateScale = true;
}
if (event.getAction() == MotionEvent.ACTION_UP) {
touchStateScale = false;
return false;
}
return true;
}
};

OnTouchEvent logic for surfaceview drawing app

Below is a switch case logic for drawing on the surface, it doesn't draw properly. Not sure where am i going wrong. EDIT: class was removed as it was not helpful.
#Override
public boolean onTouchEvent(MotionEvent event) {
synchronized (mThread.getSurfaceHolder()) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_DOWN:
currentDrawingPath = null;
currentDrawingPath = new DrawingPath();
currentDrawingPath.getPath().moveTo(eventX, eventY);
startX = eventX;
startY = eventY;
canvasPaths.add(currentDrawingPath);
//invalidate();
break;
case MotionEvent.ACTION_MOVE:
float dx = Math.abs(eventX - startX);
float dy = Math.abs(eventY - startY);
currentDrawingPath.getPath().quadTo(startX, startY,
(eventX + startX)/2, (eventY + startY)/2);
startX = eventX;
startY = eventY;
//currentDrawingPath.getPath().lineTo(startX, startY);
break;
}
return true;
Following is the code that fixed the issue,
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
currentDrawingPath.getPath().lineTo(event.getX(), event.getY());
break;
case MotionEvent.ACTION_DOWN:
currentDrawingPath = new DrawingPath();
currentDrawingPath.setPaintForPath();
// update the starting point of the new path
currentDrawingPath.getPath().moveTo(event.getX(), event.getY());
currentDrawingPath.getPath().lineTo(event.getX(), event.getY());
canvasPaths.add(currentDrawingPath);
break;
case MotionEvent.ACTION_MOVE:
currentDrawingPath.getPath().lineTo(event.getX(), event.getY());
break;
}
return true;

MotionEvent.Action_up called to early

I am having an issue with MotionEvent.ACTION_UP The event is called before I lift my finger.
Here is the code I am using. What should I change? Thanks for any help!
public boolean onTouchEvent(MotionEvent e) {
switch(e.getAction()) {
case MotionEvent.ACTION_DOWN:
if(checkColide(e.getX(), e.getY())) {
isFootballTouched = true;
downT = c.MILLISECOND;
downX = e.getX();
}
break;
case MotionEvent.ACTION_MOVE:
//moveFootball(e.getX(), e.getY());
break;
case MotionEvent.ACTION_UP:
upT = c.MILLISECOND;
upX = e.getX();
getVelocity();
break;
}
return false;
}
Try returning true if one of this 3 case occurred
public boolean onTouchEvent(MotionEvent e) {
switch(e.getAction()) {
case MotionEvent.ACTION_DOWN:
if(checkColide(e.getX(), e.getY())) {
isFootballTouched = true;
downT = c.MILLISECOND;
downX = e.getX();
}
return true;
case MotionEvent.ACTION_MOVE:
//moveFootball(e.getX(), e.getY());
return true;
case MotionEvent.ACTION_UP:
upT = c.MILLISECOND;
upX = e.getX();
getVelocity();
return true;
}
return false;
}
Probably you should return true from the onTouchEvent(). Returning false means that you're not interested in receiving events to this View anymore. Hope this helps.

Categories

Resources