Search code examples
androidviewtouch-eventontouchlistener

Is there a offset between onTouchEvent and onTouchListener?


I have developed a game that shoots when player touches the screen by using onTouchListener for my custom SurfaceView and Thread.

But now I want to change the approach and instead of onTouchListener I added onTouchEvent to the SurfaceView o my Activity.

The problem is that I get some kind of offset when I click on the emulator.
Everything is working great, except that offset keeps appearing and I don't understand why.

Also let me mention that my app is running in landscape mode, maybe this is relevant.

I suspect that it isn't working properly because onTouchListener was added to the view and depended on it, but onTouchEvent doesn't depend on the view.

Also my view doesn't have any padding or margin properties, it is full-screen view (fill_parent).

Does anyone have any ideas on this?


Solution

  • I have done my application, and everything works correctly now, but i still do not know what the problem was.

    After lots of debugging of my application the onTouchEvent returned random Y values that were always higher than the ones that the onTouchListener returned. And i am not sure why this is hapening since my view that recognizes the onTouchListener is a full-screen view.

    So i figured out a way to get passed this by doing some math.

    The first function that the android calls is the onTouch method which gives the correct values but just for one touch. I needed it to give right values even on the MotionEvent.ACTION_MOVE so i noticed that MotionEvent.ACTION_MOVE is actually doing correctly depending on the first touch recognized by the onTouchEvent.

    So i just got the coordinate Y from the onTouch and the different coordinate with the offset from onTouchEvent calculated the difference and in every onTouchEvent from there, until the user lifts up their finger, i just subtract that difference and that gives me the correct value.

    If anyone else has this problem, and doesn't know how to fix it, here is my code, maybe it will be helpful.

    @Override
    public boolean onTouchEvent(MotionEvent arg1) {
        /*you can only touch when the thread is running*/
        if(game.state() != STATE_PAUSE){
            if(arg1.getAction() == MotionEvent.ACTION_DOWN){
                coordinateX = arg1.getX();
                coordinateY = arg1.getY();
                differenceY = Math.abs(coordinateY - touchedY);
                coordinateY = coordinateY - differenceY;
                shootingIsOkay = true;
                game.setDrawCircle(coordinateX,coordinateY);
            }
            if(arg1.getAction() == MotionEvent.ACTION_MOVE){
                coordinateX = arg1.getX();
                coordinateY = arg1.getY();
                coordinateY = coordinateY - differenceY;
                shootingIsOkay = true;
                game.setDrawCircle(coordinateX,coordinateY);
            }
            if(arg1.getAction() == MotionEvent.ACTION_UP){
                    shootingIsOkay = false;
            }
        }
        return false;
    }
    

    And the onTouch method that is called from the onTouchListener that depends on the view is just simple, here:

    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
        touchedY = arg1.getY();
        return false;
    }
    

    If anyone knows how to fix this problem, please post your answer, i am very curious why this is happening