0

currently i am looking for ideas how to implement a dwelling time to the onTouch function. Normally the onTouch event does only process the events Up, Down, and Motion.

I want the user to be able to touch down anywhere, after that he can swipe to an object on the screen and if he stays still (=little to no movements) for a certain time an action is fired.

Is there a ways to get events for not moving while touching or any other type of event i can use for this behaviour? My current solution is rather... ugly

     touch = new Vector2D(event.getX(),event.getY());
    //if (not moving && touching && (System.nanoTime-currentNanoSeconds) > Value)
    if(event.getAction()==MotionEvent.ACTION_MOVE){
        currentNanoSeconds=System.nanoTime();
        System.out.println(currentNanoSeconds);
    }
    if(event.getAction()== MotionEvent.ACTION_UP){
        currentAngle=(float) angleBetween2Lines(midPoint, lineEnd, touch);
        System.out.println((System.nanoTime()-currentNanoSeconds)/1000000);
        touching=false;
        checkTargets();
        //forces redraw!
        invalidate();

    }
    if(event.getAction()==MotionEvent.ACTION_DOWN)
        touching=true;
    return true;

thanks in advance.

Faust
  • 57
  • 1
  • 7

1 Answers1

1

I think you just need to add some spatial conditions to this answer. Something like (very loosely):

maxMovement = ?;

final Handler handler = new Handler(); 
Runnable mLongPressedinSameArea = new Runnable() { 
public void run() { 
    if (*pseudo code*: the deltas between up/down X and Y are not greater than maxMovement){
         Log.i("", "Long press in same area!");
    }
 }   
};

@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView){
if(event.getAction() == MotionEvent.ACTION_DOWN)
    downX = event.getX();
    downY = event.getY();
    handler.postDelayed(mLongPressedinSameArea, 1000);//1 second linger time.
if(event.getAction() == MotionEvent.ACTION_UP))
    upX = event.getX();
    upY = event.getY();
    handler.removeCallbacks(mLongPressedinSameArea);
return super.onTouchEvent(event, mapView);

}

Community
  • 1
  • 1
JASON G PETERSON
  • 2,193
  • 1
  • 18
  • 19
  • thanks this did the trick, my solution didnt consider the slight movement on smaller devices when just holding down the touchscreen. – Faust Dec 17 '14 at 09:22