Ich brauchte ein einzelnes tap touch erkennen in meiner benutzerdefinierten Ansicht der ontouch-Methode. Ich versuchte, die x- und y-Werte in ACTION-DOWN und ACTION-UP zu erhalten, und in ACTION-UP gab ich eine Bedingung, dass, wenn a die Werte von X und Y in ACTIONDOWN und ACTION-UP gleich sind, es als einen einzigen Tap nehmen .singletap Touch erkennen in Ontouch-Methode der Ansicht
Mein Code ist wie
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (!mSupportsZoom && !mSupportsPan) return false;
mScaleDetector.onTouchEvent(ev);
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();
mLastTouchX = x; //here i get x and y values in action down
mLastTouchY = y;
mActivePointerId = ev.getPointerId(0);
break;
}
case MotionEvent.ACTION_MOVE: {
final int pointerIndex = ev.findPointerIndex(mActivePointerId);
final float x = ev.getX(pointerIndex);
final float y = ev.getY(pointerIndex);
if (mSupportsPan && !mScaleDetector.isInProgress()) {
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
mPosX += dx;
mPosY += dy;
//mFocusX = mPosX;
//mFocusY = mPosY;
invalidate();
}
mLastTouchX = x;
mLastTouchY = y;
break;
}
case MotionEvent.ACTION_UP: {
final float x = ev.getX();
final float y = ev.getY();
touchupX=x; //here is get x and y values at action up
touchupY=y;
if(mLastTouchX == touchupX && mLastTouchY == touchupY){ //my condition if both the x and y values are same .
PinchZoomPanActivity2.tapped1(this.getContext(), 100); //my method if the singletap is detected
}
else{
}
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_CANCEL: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_POINTER_UP: {
final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
>> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = ev.getPointerId(pointerIndex);
if (pointerId == mActivePointerId) {
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastTouchX = ev.getX(newPointerIndex);
mLastTouchY = ev.getY(newPointerIndex);
mActivePointerId = ev.getPointerId(newPointerIndex);
}
break;
}
}
return true;
}
folgt, aber ich kann es nicht getan. Ich meine, bei jeder Aktion wird meine Methode aufgerufen. selbst wenn die x- und y-Werte von actionup und actiondown nicht gleich sind. und ich denke, ich muss auch etwas Abstand zum Singletap legen, während wir mit dem Finger auf dem Bildschirm berühren. Kann mir jemand einige Vorschläge machen?
verwenden Es wird nie an genau derselben Stelle sein - dein Finger wird sich immer leicht bewegen. Verwenden Sie eine Bereichsprüfung - stellen Sie sicher, dass abs (upLoc-downLoc) <5. –
Ich brauche Bereichsprüfung, aber ich habe nicht verstanden, Ihre "abs (upLoc-downLoc) <5". –
abs = absoluter Wert. Das heißt also, wenn sich der Standort um weniger als 5 Pixel geändert hat. –