【问题标题】:How to get correct coords(event.getX() / event.getY()) of Object with OnTouchListener如何使用 OnTouchListener 获取对象的正确坐标(event.getX() / event.getY())
【发布时间】:2015-12-03 09:47:22
【问题描述】:

我有这个:

而我真正想要的是通过对象的 坐标(X,Y) 和 OnTouchListener 显示位图的一部分(橙色 square,圆点位于中心)。

所以问题是我想绘制图像的一部分,就像它在图像上显示的那样(红色方块“我想要显示的区域”)。
所以在这种情况下,例外结果是(位图的一部分):

目前我正在这样做:

public boolean onTouch(View view, MotionEvent event) {

    switch (event.getAction()) {
        case MotionEvent.ACTION_MOVE:

            Bitmap mBitmap = Bitmap.createBitmap(sourceBitmap,view.getX() - view.getWidth(),view.getY()-view.getHeight(),250,250);
            //other stuff to fill this portion of bitmap
            break;
    }

    return true;
}
}

这是不正确的。

我怎样才能做到这一点? 尝试了很多公式,但没有运气。有什么建议么?

PS 据我了解,event.getX()/event.getY() 正在获取相对坐标(我不清楚从对象获得的确切坐标带有触摸监听器的图像视图(橙色正方形,中心有点),我的意思是它得到这个对象的 centerTop.Left 角(X,Y))?

【问题讨论】:

  • 任何代码示例?我认为 getY() 在这种情况下被倒置 IIRC 这可能会给您带来问题?
  • @Hughzi 好的,现在添加代码 sn-ps。感谢您的回复!
  • 如果你登录你的event.getX()/event.getY(),你会得到什么?
  • @DanChaltiel 我正在获取对象的坐标。还是你的意思?
  • @VetaLio 我认为您需要执行 getWidth - getX 就好像您的宽度为 100 并且您当前在 -50 处触摸 50?

标签: android bitmap coordinates ontouchlistener magnify


【解决方案1】:

抱歉,虽然我想更详细地寻找解决方案,但我想我找到了它并且我正在工作,所以你可以试试这个:

使用event.getX() 而不是view.getX()view.getX() 返回您正在触摸的视图的位置,而不是您感兴趣的触摸事件。

这是可能会反转的 Y 坐标。

所以event.getX()是触摸事件的位置。

event.getY() 是倒置的 Y 位置,因此 getHeight() - event.getY() 会为您提供所需的 Y 位置。

编辑

在 MOTION_MOVE 的情况下,getX 和 getY 返回最新数据并保留历史数据。我想最近的一张是最有价值的,因为那是你要画的地方。

在这种情况下,请使用文档中的批处理报价:

批处理 - http://developer.android.com/reference/android/view/MotionEvent.html

为了提高效率,带有 ACTION_MOVE 的运动事件可能会一起批处理 单个对象内的多个运动样本。最新的 指针坐标可使用 getX(int) 和 getY(int) 获得。 批次中较早的坐标可使用 getHistoricalX(int, int) 和 getHistoricalY(int, int)。该坐标 只有在它们比现在更老的情况下才是“历史的” 批次中的坐标;然而,它们仍然不同于任何 先前运动事件中报告的其他坐标。处理所有 批次中坐标按时间顺序,先消费历史 坐标然后消耗当前坐标。

【讨论】:

  • 是的,但是我在 ACTION_MOVE 中做一些事情,使用 event.getY()/ event.getX() 是没有意义的,因为它们是相同的。我们正在移动形状(图像视图)。
  • @hugzhi 你可以尝试这样做,因为 ACTION_MOVE 不会改变 event.getX()。需要获取 view.getX() :(
【解决方案2】:

你必须得到这样的坐标,它会给你参考视图而不是屏幕的坐标。

                float xCord = event.getRawX()-v.getX();
                float yCord = event.getRawY()-v.getY();

【讨论】:

    【解决方案3】:

    所以解决这个问题很简单。
    解决方案
    我有一个带有 OnTouchListener 的对象(橙色正方形)(例如,他是一个大小为 50dp Height/ 50dp Width 的四边形)。
    所以 view.getX()/view.getY() 采用对象(具有 OnTouchListener)的相对坐标(中心)。
    所以我需要什么:

    //imageview parameters ,where i will show this part of bitmap
    int Width = img.getWidth();
    int Height = img.getHeight();
    //after this,i make margin of my coords,via this simple formula -> Current Coord X - Width / 2 (same for Y)
     Bitmap mBitmap = Bitmap.createBitmap(sourceBitmap,view.getX() - Width / 2,view.getY()-Height / 2,Width,Height);  
    

    仅此而已。
    享受吧!

    【讨论】:

      【解决方案4】:

      遗憾的是,如果您想真正解决视图范围内的事件,则没有简单的方法可以解决此问题。这仍然在某些硬件上的 Android Pie 上发生。解决奇数触摸事件的唯一方法是在渲染视图位置之前比较最后 3 个事件(如果在 ACTION_MOVE 期间没有奇数移动)。下面是在圆形视图上检查事件时的示例。假设您创建了一个 new double[3] 临时变量:

        /**
           * This detects if your finger movement is a result of an actual raw touch event of if it's from a view jitter.
           * This uses 3 events of rotation as temporary storage so that differentiation between swapping touch events can be determined.
           *
           * @param newRotationValue
           */
          private boolean isRotationTouchEventConsistent(final double newRotationValue) {
              double evalValue = newRotationValue;
      
              if (Double.compare(newRotationStore[2], newRotationStore[1]) != 0) {
                  newRotationStore[2] = newRotationStore[1];
              }
              if (Double.compare(newRotationStore[1], newRotationStore[0]) != 0) {
                  newRotationStore[1] = newRotationStore[0];
              }
      
              newRotationStore[0] = evalValue;
      
              if (Double.compare(newRotationStore[2], newRotationStore[0]) == 0
                      || Double.compare(newRotationStore[1], newRotationStore[0]) == 0
                      || Double.compare(newRotationStore[2], newRotationStore[1]) == 0
      
                      //Is the middle event the odd one out
                      || (newRotationStore[0] > newRotationStore[1] && newRotationStore[1] < newRotationStore[2])
                      || (newRotationStore[0] < newRotationStore[1] && newRotationStore[1] > newRotationStore[2])
              ) {
                  return false;
              }
              return true;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-15
        • 1970-01-01
        • 2015-08-01
        • 2018-08-06
        • 2016-11-19
        • 2013-08-18
        • 2012-03-31
        • 2021-11-05
        相关资源
        最近更新 更多