繁体   English   中英

如何使用OnTouchListener获取对象的正确坐标(event.getX()/ event.getY())

[英]How to get correct coords(event.getX() / event.getY()) of Object with OnTouchListener

我有这个 :
源案例

我确切想要的是通过OnTouchListener(中心带有点的橙色正方形 )通过对象的坐标 (X,Y)显示位图的一部分。

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

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()正在获得相对坐标(对我来说还不清楚,从带有触摸监听器的图像视图对象中获得的确切坐标是什么(中心带有点的橙色正方形),我的意思是获取此对象的中心Top.Left corners(X,Y))?

抱歉,在详细了解解决方案时,我认为我找到了它,并且我正在工作,所以您可以尝试以下方法:

使用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)访问批处理中的较早坐标。 坐标仅在其早于批处理中的当前坐标的范围内才是“历史的”。 但是,它们仍然不同于先前运动事件中报告的任何其他坐标。 要按时间顺序处理批处理中的所有坐标,请首先使用历史坐标,然后使用当前坐标。

您必须像这样获得坐标,它将为您提供参考以查看屏幕而不是屏幕。

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

因此,解决此问题很简单。
解决方案
我有一个带有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);  

仅此而已。
请享用!

可悲的是,如果您想在视图范围内真正解决该事件,没有简单的解决方法。 在某些硬件上的Android Pie上仍然会发生这种情况。 解决奇数触摸事件的唯一方法是,如果在ACTION_MOVE期间没有奇异动作,则比较最后3个事件,然后再放置视图位置。 以下是在圆形视图上检查事件的示例。 假设您创建了一个新的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;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM