Android firing onTouch event for multiple ImageViews

后端 未结 2 537
借酒劲吻你
借酒劲吻你 2020-12-20 07:25

I have several ImageViews, and I want the onTouch event to fire for each of them when I drag my finger across multiple images. At present the onTouch event is only firing on

2条回答
  •  萌比男神i
    2020-12-20 07:44

    I think you need to use move event rather than touch event and compare getX and getY with the location of your views.

         @Override
        public boolean onTouchEvent(MotionEvent ev) {
    
            final int action = ev.getAction();
    
            switch (action) {
    
                // MotionEvent class constant signifying a finger-down event
    
                case MotionEvent.ACTION_DOWN: {
                    break;
                }
    
                // MotionEvent class constant signifying a finger-drag event  
    
                case MotionEvent.ACTION_MOVE: {
    
                        X = ev.getX();
                        Y = ev.getY();
                        //compare here using a loop of your views
                        break;
    
                }
    
                // MotionEvent class constant signifying a finger-up event
    
                case MotionEvent.ACTION_UP:
    
                    break;
    
            }
            return true;
        }
    

提交回复
热议问题