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

佐手、 提交于 2019-11-28 14:37:09

Sorry, whilst looking in more detail coming up with solution i think i found it and I am at work so can you try this:

Use event.getX() not view.getX(). the view.getX() returns position of the view you are touching not the touch event you are interested in.

This is the Y coordinate which will likely be inverted.

so event.getX() is the position of the touch event.

event.getY() is the inverted Y position so getHeight() - event.getY() will give you the Y position you are after.

EDIT

In the case of MOTION_MOVE getX and getY return the most recent and maintains historical data. i guess the most recent one is the most valuable as that's where you will be drawing.

In that case use batching quote from documentation:

Batching - http://developer.android.com/reference/android/view/MotionEvent.html

For efficiency, motion events with ACTION_MOVE may batch together multiple movement samples within a single object. The most current pointer coordinates are available using getX(int) and getY(int). Earlier coordinates within the batch are accessed using getHistoricalX(int, int) and getHistoricalY(int, int). The coordinates are "historical" only insofar as they are older than the current coordinates in the batch; however, they are still distinct from any other coordinates reported in prior motion events. To process all coordinates in the batch in time order, first consume the historical coordinates then consume the current coordinates.

You have to get the coordinates like this , it will give you coordinates with reference to view not the screen.

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

So it was simple to resolve this problem.
Solution:
I have an object(orange square) with OnTouchListener(for e.g. he's an quad with size 50dp Height/ 50dp Width).
So view.getX()/view.getY() takes relative coords(center) of object(that have OnTouchListener).
So what i needed:

//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);  

And that is all.
Enjoy!

Sadly there's no easy way of solving this if you want to really address the event within the bounds of the view. This is still occurring on Android Pie on some hardware. The only way to address odd touch events is to compare the last 3 events if there are no odd movements during ACTION_MOVE before rending the position of the view. Below is a sample when checking the event on a circular view. Assumption is that you created a new double[3] temporary variable :

  /**
     * 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;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!