DoubleTap detection on Android “View” component

2019-09-09 22:27发布

问题:

What I need to achieve here is as following 1) superimpose a transperent "dot" on the PNG image ; where user has clicked. 2) superimpose a transperent "Big circle", when user is holding touch for long. 3) upon Doble click "Clear the screen"

Primarily I am using "onTouchEvent" for click-event detection... and "geastureDetector" for doubleTap detection... but not getting the desired result.

These are the 2 implementations I am trying out

This first approach works fine ... But the click event generated is with some deviation... I mean to say the click event captured is always Offset by around 50 pixels in Y direction.... I am not able to figure out why this should happen..

class Tileview extends Activity implements GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener {

onCreate(){
  //add the View Thing here.
}

@Override
public void onLongPress(MotionEvent e) {
   // "longPress Detected"
}

@Override
public boolean onDoubleTap(MotionEvent e) {
  // "longPress Detected"
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    final int action = event.getActionMasked();
    switch(action){
    case MotionEvent.ACTION_DOWN:           
        mClickX = event.getX();
        mClickY = event.getY();     

    }
    myTileView.invalidate();
    return mGestureDetector.onTouchEvent(event);
}



private class MyTileView extends View{

protected void onDraw(Canvas canvas){
   // Canvas & Paint stuff - for translucent cicle 
   c.drawCircle(mClickX, mClickY, 75.0f, p);
   }
}

The other approach is able to superimpose the image propely at proper location; but when I add the DoubleTap implementation in the "View" instead of Activity .. the doubleTap / geastureListeners would never fire .... Only change I did in that case was to get the onTouchEvent() - and corrsponding implementation in "view" class....

class TileActivity extends Activity {

   OnCreate()
          { 
          // Bla Bla Bla       
          }

    private class TileView extends View implements OnDoubleTapListener, OnGestureListener{
            @override
            onTouchEvent(){
                    // get co-ordinates here from MotionEvent
            }

    @Override
    public boolean onDoubleTap(MotionEvent e) {

                  // THIS WON'T EVEN GET FIRED
                  // In this case the doubleTap detection is not working

        clearEntireScreen = true;
        return false;
    }
    }
}

回答1:

Maybe the problem is that onTouchEvent is not returning true when the gesture detector is not detecting a double-tap or long-press? I would put the call to mGestureDetector.onTouchEvent at the start of onTouchEvent, and only go on to do the ACTION_DOWN processing if it returns false. And in that case, onTouchEvent should be sure to return true:

@Override
public boolean onTouchEvent
  (
    MotionEvent event
  )
  {
    boolean Handled = mGestureDetector.onTouchEvent(event);
    if (!Handled)
      {
        final int action = event.getActionMasked();
        switch(action)
          {
        case MotionEvent.ACTION_DOWN:           
            mClickX = event.getX();
            mClickY = event.getY();
            Handled = true;
        break;
          } /*switch*/
        myTileView.invalidate();
      } /*if*/
    return
        Handled;
  } /*onTouchEvent*/


回答2:

It is RESOLVED now...!! Seems the best way to tackle the issue was to create gestureDetector class and implement "SimpleOnGestureListener"..

I had to tweak the implementation a little bit... but the skeleton code is much more similar to what is explained here in this post... Fling gesture detection on grid layout

I had to override LongPress / onDoubleTap / onSigleTapUp / onSingletapconfirmed methods to achieve the desired results...

Thanks for the help... !!