Android - Intercept and pass on all touch events

后端 未结 5 1977
名媛妹妹
名媛妹妹 2021-02-02 15:47

I have an overlay ViewGroup that is the size of the screen which I want to use to show an effect when the user interacts with the app, but still passes the onTouch event to any

5条回答
  •  日久生厌
    2021-02-02 16:02

    Here's what I did, building on Dori's answer I used onInterceptTouch . . .

    float xPre = 0;
    
    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if(Math.abs(xPre - event.getX()) < 40){
            return false;
        }
        if(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_UP  || event.getAction() == MotionEvent.ACTION_MOVE){
            xPre = event.getX();
        }
        return true;
    }
    

    Basically, when any important movement is made, remember where it happened, via xPre; then if that point is close enough to the next point to be say, an onClick/onTouch of the child view return false and let onTouch do it's default thing.

提交回复
热议问题