【问题标题】:Android: Is there a way to check where on an ImageView the user has clicked? [duplicate]Android:有没有办法检查用户点击了 ImageView 的哪个位置? [复制]
【发布时间】:2018-05-14 10:14:16
【问题描述】:

具体来说,有没有办法获取用户点击相对于ImageView左上角的坐标?

【问题讨论】:

标签: android imageview


【解决方案1】:

你可以实现onTouchListener来获取坐标,如下:

imageButton.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
       int x = (int) event.getX();
       int y = (int) event.getY();
});

【讨论】:

  • 好的,我明白了。但这是否给出了相对于整个屏幕左上角或视图本身左上角的 x 和 y 坐标?我正在寻找后者。
  • @Aryaman 如果是屏幕位置,您可以添加View v屏幕坐标的左上角
  • @Aryaman getX() 返回相对于视图的接触点
猜你喜欢
  • 2019-09-21
  • 1970-01-01
  • 1970-01-01
  • 2019-09-08
  • 2012-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-24
相关资源
最近更新 更多