OnClickListener - x,y location of event?

后端 未结 4 2202
难免孤独
难免孤独 2020-11-28 09:28

I have a custom view derived from View. I\'d like to be notified when the view is clicked, and the x,y location of where the click happened. Same for long-clicks.

L

相关标签:
4条回答
  • 2020-11-28 10:00

    Full example

    The other answers are missing some details. Here is a full example.

    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 click listener
            View myView = findViewById(R.id.my_view);
            myView.setOnTouchListener(touchListener);
            myView.setOnClickListener(clickListener);
        }
    
        // 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.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(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);
            }
        };
    }
    

    Summary

    • Add a class variable to store the coordinates
    • Save the X,Y coordinates using an OnTouchListener
    • Access the X,Y coordinates in the OnClickListener
    0 讨论(0)
  • 2020-11-28 10:01

    You can make an extension function in Kotlin, like this:

    fun View.setOnClickListenerWithPoint(action: (Point) -> Unit) {
        val coordinates = Point()
        val screenPosition = IntArray(2)
        setOnTouchListener { v, event ->
            if (event.action == MotionEvent.ACTION_DOWN) {
                v.getLocationOnScreen(screenPosition)
                coordinates.set(event.x.toInt() + screenPosition[0], event.y.toInt() + screenPosition[1])
            }
            false
        }
        setOnClickListener {
            action.invoke(coordinates)
        }
    }
    

    If you need a long click - just replace setOnClickListener

    I use getLocationOnScreen() because I need coordinates for RecyclerView's ViewHolder

    0 讨论(0)
  • 2020-11-28 10:09

    Thank you. That was exactly what I was looking for. My code now is:

    imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN){
                    textView.setText("Touch coordinates : " +
                            String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
                }
                return true;
            }
        });
    

    which does precisely what Mark asked for...

    0 讨论(0)
  • 2020-11-28 10:21

    Override onTouchEvent(MotionEvent ev)

    Then you can do:

    ev.getXLocation()
    

    Or something like that. Have a butches.

    0 讨论(0)
提交回复
热议问题