Android: Detect if user touches and drags out of button region?

前端 未结 9 2027
孤城傲影
孤城傲影 2020-12-04 16:33

In Android, how can we detect if a user touches on button and drags out of region of this button?

9条回答
  •  青春惊慌失措
    2020-12-04 16:50

    While the answer from @FrostRocket is correct you should use view.getX() and Y to account for translations changes as well:

     view.getHitRect(viewRect);
     if(viewRect.contains(
             Math.round(view.getX() + event.getX()),
             Math.round(view.getY() + event.getY()))) {
       // inside
     } else {
       // outside
     }
    

提交回复
热议问题