The basic idea of Roflcoptr is correct, but even if you move the pointer without a long click, onLongClick is called. To avoid this, you can use this code:
final int NONE=0;
final int DRAG=1;
final int LONG_PRESS=2;
int mode=NONE;
PointF start=new PointF();
public boolean onLongClick(View v) {
if(mode==NONE) mode=LONG_PRESS;
return false;
}
public boolean onTouch(View v, MotionEvent event){
switch(event.getAction() & MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_DOWN:
start.set(event.getX(),event.getY());
break;
case MotionEvent.ACTION_UP:
mode=NONE;
break;
case MotionEvent.ACTION_MOVE:
if(mode==NONE && getDistance(event.getX(),event.getY(),start.x,start.y)>30d) mode=DRAG;
if(mode==DRAG){
}
if(mode==LONG_PRESS){
}
break;
}
}
private double getDistance(float x1,float y1,float x2,float y2){
float dx=Math.abs(x1-x2);
float dy=Math.abs(y1-y2);
return Math.sqrt(dx*dx+dy*dy);
}
Hope this helps someone)
source
share