i am trying to do finger swipe but it is not detected .i am trying to draw a path which describes the area through which finger swipe but only when i swipe with force then only swipe get detected and else not detected
public boolean onTouchEvent(MotionEvent event) {
if (System.currentTimeMillis() - lastClick > 500) {
lastClick = System.currentTimeMillis();
synchronized (getHolder()) {
int eventaction = event.getAction();
if(event.getPressure()>0.00001||event.getSize()>0.00001)
{
float a,b,c;
switch(eventaction)
{
case MotionEvent.ACTION_DOWN:
xdown = event.getX();
ydown =event.getY();
pres=true;
break;
case MotionEvent.ACTION_MOVE:
if(pres)
{
xmove = event.getX();
ymove =event.getY();
a = (float) Math.sqrt(Math.pow(xdown - xmove,2) +Math.pow( ydown - ymove,2));
b=(float) (xdown-(((xdown - xmove) / a) * 150));
c=(float) (ydown-((( ydown - ymove)/a)*150));
move.moveTo(xdown, ydown);
move.lineTo(b, c);
pres=false;
lmove=true;
}
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_POINTER_UP:
move.reset();
xmove = 0;
ymove =0;
xdown=0;
ydown=0;
lmove=false;
pres=false;
break;
default:
return true;
}
}
}}
return true;
}
Android has a class for that. Check http://developer.android.com/reference/android/view/GestureDetector.html
I believe it is one of in touch events
See the code here: Simple swipe gesture to activity tutorial?
Using a SimpleOnGestureListener is easiest, and if you override the onFling method, it's easy/
Related
i have problem with onTouch. I have GridLayout where i have 3x3 buttons like a image below.
B1,B2 -- is the Button view. My problem is, when i put 3 fingers on the screen at the same time and move all of them down then my phone detect it like threeSwipeGesture and make screenshot. How to avoid phone to detect gesture?
TIP: When i put 3 fingers one by one (tap 1 - wait second - tap 2 - wait second - tap 3) then i can move my fingers and gesture is not detected.
#Override
public boolean onTouch(View view, MotionEvent event) {
int action = event.getActionMasked();
int index = event.getActionIndex();
int x = -1;
int y = -1;
switch (action) {
case MotionEvent.ACTION_MOVE:
return false;
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_OUTSIDE:
case MotionEvent.ACTION_CANCEL:
gridLayout.setBackgroundColor(Color.parseColor("#df4930"));
set.clear();
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
x = (int) event.getX(index);
y = (int) event.getY(index);
for (int i = 0; i < gridLayout.getChildCount(); i++) {
View button = gridLayout.getChildAt(i);
Rect outRect = new Rect(button.getLeft(), button.getTop(), button.getRight(), button.getBottom());
if (outRect.contains(x, y)) {
set.add(button.getTag());
}
}
if (set.size() == 4) {
Log.d(DEBUG_TAG, set.toString());
gridLayout.setBackgroundColor(Color.parseColor("#ffc120"));
}
}
return true;
}
Here is error log in the console when gesture is detected.
D/ViewRootImpl: cancle motionEvent because of threeGesture detecting
I have a problem with my android emulator, I have a custom view and for controlling the touch event I use onTochEvent in my class. so for different kind of events (ex:down, up, move ...), I have righted a case. for down and up the emulator shows no problem but with the move, there is no action. on my phone, everything is just fine! i have tried different kinds of API too but didn't work. this is my onTouch code :
#Override
public boolean onTouchEvent(MotionEvent event) {
boolean result = super.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
xd = event.getX();
yd = event.getY();
break;
case MotionEvent.ACTION_MOVE:
float xm = event.getX();
float ym = event.getY();
if(card.left < xm && card.right > xm){
if(card.top < ym && card.bottom > ym){
card.top += yd - ym;
card.left += xd - xm;
card.right += xd - xm;
card.bottom += yd - ym;
postInvalidate();
}
}
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_CANCEL:
break;
case MotionEvent.ACTION_OUTSIDE:
break;
default:
}
return result;
}
android studio 3.3 api 24. Thanks.
The view needs to return true on first ACTION_DOWN event, only then it will receive successive touch events.
return super.onTouchEvent(event); only when you don't want to handle any particular kind of touch event.
I have code something like below:
switch (ev.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
pinching = false;
break;
case MotionEvent.ACTION_MOVE:
if (pinching || ld > 30.0f) {
pinching = true;
final float dxk = 0.5f * (lastdx1 + lastdx2);
final float dyk = 0.5f * (lastdy1 + lastdy2);
if(zoom * d / (d - dd) >= 4.0){
pinching = false;
scrolling = false;
} else {
smoothZoomTo(Math.max(1.0f, zoom * d / (d - dd)), dxk, dyk, true);
}
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
ev.setAction(MotionEvent.ACTION_CANCEL);
super.dispatchTouchEvent(ev);
break;
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_UP:
default:
pinching = false;
break;
}
here i am trying to put my two fingers down after zooming.My requirement is not to move when maxzoom is reached. But right now everytime action move getting called on action pointer down event.
How can i skip action move in this scenario? Any help much appreciated.
In Case you want to perform ACTION_MOVE for only single touch,
then use
ev.getPointerCount();
This gives the number of pointers. If this count is more than 1 don't perform the move action.
I use the onTouchEvent method to be able to have a drag&drop event and a click event. I test the result on my Nexus 7 (2012), it's working really great but on my smarphone a Asus Zenfone, it's not and I can't see why, here is my code :
button.setOnTouchListener(new View.OnTouchListener() {
private float startX, startY;
private long startClickTime;
#Override
public boolean onTouch(final View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
startClickTime = Calendar.getInstance().getTimeInMillis();
break;
case MotionEvent.ACTION_MOVE:
if (v instanceof Button) {
Button button = ((Button) v);
if (button.getText().length() == 0) return true;
ClipData clipData = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new DragShadowBuilder(v, false, scale);
v.startDrag(clipData, shadowBuilder, v, 0);
DataHolder.getInstance().setCurrentText(button.getText().toString());
button.setText("");
}
return true;
case MotionEvent.ACTION_UP:
float endX = event.getX();
float endY = event.getY();
long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
if (isAClick(startX, endX, startY, endY) && clickDuration < CLICK_DURATION) {
v.performClick();
}
return true;
}
return false;
}
});
Did someone see why my click is not triggered ?
Edit : the screenshot
This is my suggestion - Go to your phone settings there is an option to show layout boundaries you can set it on and off - you can see where you are clicking when this options is on .
I take no responsibility on what might happen to your phone yet this is how i always solve those problems
I finally succeed to make my code working. I just added in the Action Move if the detection of a click, if it's not and only if it's not I begin a drag. That's it.
I try to fix an issue which appears in my code when I added multitouch functions in my app.
The problem seems to come from ACTION_POINTER_DOWN :
private float oldDist = 0;
backCard.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent me) {
switch(me.getAction()){
case MotionEvent.ACTION_DOWN:
firstX = (int) me.getX();
case MotionEvent.ACTION_POINTER_DOWN:
if(me.getPointerCount() >= 2){
oldDist = getSpacing(me);
System.out.println(oldDist);
}
break;
case MotionEvent.ACTION_MOVE:
float newDist = getSpacing(me);
if(newDist - oldDist > 200 && oldDist != 0){
System.out.println("Enabled");
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
break;
}
return true;
}
private float getSpacing(MotionEvent me){
float difx = me.getX(0) - me.getX(1);
float dify = me.getY(0) - me.getY(1);
float spacing = (float) Math.sqrt(difx*difx + dify*dify);
return spacing;
}
});
When I use it without the getPointerCount() condition in ACTION_POINTER_DOWN, I have an out of range error. But if I use the condition, the log doesn't show anything I print in the code. (Of course I use 2 fingers ! :) ), so the condition is never true, even if several fingers touch the screen at the same time.
How can I fix that ? Thank you.
My device is a GS3.
Use me.getActionMasked() instead of me.getAction()