1. 程式人生 > >Android判斷一個點是否在矩形區域內

Android判斷一個點是否在矩形區域內

ole main art int 拖拽 div help mov @override

個人遇到的問題判斷按鈕的點擊事件還是滑動事件

   private boolean button1Down = false;
   private boolean button2Down = false;

   private boolean pointInRect(float x, float y, int w, int h) {
            if (x < 0) {
                return false;
            }
            if (x > w) {
                return false;
            }
            if (y < 0) {
                return false;
            }
            if (y > h) {
                return false;
            }
            return true;
    }

判斷按鈕坐標是否在矩形區域內

 tv_button_group.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            button1Down = pointInRect(event.getX(), event.getY(), v.getWidth(), v.getHeight());
                            break;
                        case MotionEvent.ACTION_MOVE:
                            for(int i=0; i<event.getHistorySize(); i++) {
                                if(!pointInRect(event.getHistoricalX(i), event.getHistoricalY(i), v.getWidth(), v.getHeight())) {
                                    button1Down = false;
                                    break;
                                }
                            }
                            break;
                        case MotionEvent.ACTION_UP:

                            // 判斷是否為點擊
                            if (button1Down && pointInRect(event.getX(), event.getY(), v.getWidth(), v.getHeight())) {
                                if (initdata.get(position).isIsJoined()) {
                                    SessionHelper.startTeamSession(MainActivity.this, initdata.get(position).getGroupId());
                                } else {
                                    joinGroup(initdata.get(position).getGroupId());
                                }
                            } else {

                                if (event.getY() < -300) {
                                    recyclerView.setVisibility(View.GONE);
                                    iv_data.setVisibility(View.GONE);
                                    ll_bottom.setVisibility(View.GONE);
                                    vw_view.setVisibility(View.GONE);
                                    rl_deta.setVisibility(View.GONE);
                                    mTitleWidget.setVisibility(View.GONE);
                                    im_fanhui.setVisibility(View.VISIBLE);
                                    rl_group.setVisibility(View.VISIBLE);
                                    itData(mLongitude, mLatitude);
                                    uiSettings.setScrollGesturesEnabled(true); //禁止拖拽
                                    isoncl = false;
                                    return true;
                                }
                            }
                            break;
                    }

                    return true;
                }
            });

Android判斷一個點是否在矩形區域內