Android: How to call onTouch only after onLongClick?

I have an image that can be moved and scaled using a gesture gesture .. All this is done inside onTouch(). I want to limit this and make it movable (and scalable) only after the user has made a long click on the image. How should I do it?

+5
source share
3 answers

Register LongCLickListener . If a long click is recognized, set the flag to true.

The OnTouch method allows scaling and moving only if the flag is set to true. After moving and scaling, set the flag to false again.

Here are a few pseudo codes:

public class MyActivity extends Activity {

   private boolean longClick = false;

   public boolean onTouch(View v, MotionEvent event) {
      if (longClick) {
         // do scaling and moving ...
         longClick = false;
      }
      return false;
   }

   public boolean onLongClick(View v) {
      longClick = true;
      return false;
   }
}
+9
source

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; //it helps to avoid unwanted onLongClick
    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()); //point where we started from
            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 we really move our pointer, then start the drag mode.
            //it helps to avoid unwanted moving

            if(mode==DRAG){
            //do your stuff
            }
            if(mode==LONG_PRESS){
            //do your stuff
            }
            break;
    }
}

//returns distance between 2 points
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)

+2
source

View.ontouchlistener onlongpress

    LinearLayout.LayoutParams longpressLP = new LinearLayout.LayoutParams(100,100);
    LinearLayout longpress = new LinearLayout(this);
    longpress.setBackgroundColor(Color.GREEN);
    mainlayout.addView(longpress,longpressLP);



    final View.OnTouchListener buttontouch=new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            if(event.getAction()==MotionEvent.ACTION_DOWN)
                v.setOnTouchListener(buttontouchnone);
            else
                Toast.makeText(getApplicationContext(), "Touched", Toast.LENGTH_SHORT).show();
            return false;
        }
    };
    final View.OnTouchListener buttontouchnone=new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "None Touch", Toast.LENGTH_SHORT).show();
            return false;
        }
    };
    longpress.setOnTouchListener(buttontouch);
    longpress.setOnLongClickListener(new View.OnLongClickListener() {

        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "LongClick", Toast.LENGTH_SHORT).show();
            v.setOnTouchListener(buttontouch);
            return true;
        }
    });
+2

All Articles