DoubleTap detection on Android “View” component

你说的曾经没有我的故事 提交于 2019-12-25 08:23:05

问题


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... !!



来源:https://stackoverflow.com/questions/9042522/doubletap-detection-on-android-view-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!