Android判断一个点是否在矩形区域内

个人遇到的问题判断按钮的点击事件还是滑动事件

   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判断一个点是否在矩形区域内

上一篇:vs项目中使用c++调用lua


下一篇:Swift编程语言学习1.6——可选值