开发者

Android Press LongClickListener get X, Y Coordinates, OnTouchListener

I have a button and want to use a LongClickListener, to get by pressing on the button the coordinates during changing the position of the button. How can I get in a LongClickListener or perhaps other Method the X,Y coordinates of the Click/Mouse.

I tried it with an OnTouchListener, that is working. But the problem 开发者_如何学编程is that the TouchListener reacts on each click and not how I want only on pressed.


do it like here in OnTouchListener:

OnTouchListener mOnTouch = new OnTouchListener()
{
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {            
       final int action = ev.getAction();
       switch (action & MotionEvent.ACTION_MASK) {
       case MotionEvent.ACTION_DOWN: {
          final int x = (int) ev.getX();
          final int y = (int) ev.getY();
       break;
    }
};


You have to store the last known coordinates as found in onTouch somewhere (global data for example) and read them in your onLongClick method.

You may also have to use onInterceptTouchEvent in some cases.


The solution is to

  • Add a class variable to store the coordinates
  • Save the X,Y coordinates using an OnTouchListener
  • Access the X,Y coordinates in the OnLongClickListener

The other two answers leave out some details that might be helpful, so here is a full demonstration:

public class MainActivity extends AppCompatActivity {

    // class member variable to save the X,Y coordinates
    private float[] lastTouchDownXY = new float[2];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // add both a touch listener and a long click listener
        View myView = findViewById(R.id.my_view);
        myView.setOnTouchListener(touchListener);
        myView.setOnLongClickListener(longClickListener);
    }

    // the purpose of the touch listener is just to store the touch X,Y coordinates
    View.OnTouchListener touchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            // save the X,Y coordinates
            if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
                lastTouchDownXY[0] = event.getX();
                lastTouchDownXY[1] = event.getY();
            }

            // let the touch event pass on to whoever needs it
            return false;
        }
    };

    View.OnLongClickListener longClickListener = new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {

            // retrieve the stored coordinates
            float x = lastTouchDownXY[0];
            float y = lastTouchDownXY[1];

            // use the coordinates for whatever
            Log.i("TAG", "onLongClick: x = " + x + ", y = " + y);

            // we have consumed the touch event
            return true;
        }
    };
}


@Override
public boolean performLongClick(float x, float y) {
    super.performLongClick(x, y);
    doSomething();
    return super.performLongClick(x, y);
}


i have written a class to do this. use:

new SetOnLongTouch(tv_main, new SetOnLongTouch.onLongTouchListener() {
            @Override
            public void onLongTouch(View v, float X,float Y) {
                Log.d("TEST","X:" + X + " Y:" + Y);
            }
});

Class:

import android.view.MotionEvent;
import android.view.View;

class SetOnLongTouch {
    View view;
    private float[] lastTouchDownXY = new float[2];
    onLongTouchListener listener;
    SetOnLongTouch(View v,onLongTouchListener onLongTouchListener){
        view = v;
        listener = onLongTouchListener;
        v.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    lastTouchDownXY[0] = motionEvent.getX();
                    lastTouchDownXY[1] = motionEvent.getY();
                }else if(motionEvent.getAction() == MotionEvent.ACTION_UP){
                    view.performClick();
                }
                return false;
            }
        });
        v.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                listener.onLongTouch(view,lastTouchDownXY[0],lastTouchDownXY[1]);
                return true;
            }
        });
    }
    public interface onLongTouchListener{
        void onLongTouch(View v, float X, float Y);
    }
}

if you are a newbie like me then it also gives you the idea that how these listener works and how,why and where use interfaces.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜