Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FloatView的点击事件 #11

Open
aaaaaaaazmx opened this issue Aug 22, 2024 · 0 comments
Open

FloatView的点击事件 #11

aaaaaaaazmx opened this issue Aug 22, 2024 · 0 comments

Comments

@aaaaaaaazmx
Copy link

`private static final int CLICK_THRESHOLD = 5; // 定义点击的阈值,单位是像素

@Override
public boolean onTouch(View v, MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mDownX = event.getX();
            mDownY = event.getY();
            mFirstY = Math.round(event.getRawY());
            mFirstX = Math.round(event.getRawX());

            mHandler.removeCallbacksAndMessages(mRunnable);

            resetStatus();
            isMove = false; // 重置移动标志
            break;

        case MotionEvent.ACTION_MOVE:
            float deltaX = Math.abs(x - mDownX);
            float deltaY = Math.abs(y - mDownY);
            if (deltaX > CLICK_THRESHOLD || deltaY > CLICK_THRESHOLD) {
                isMove = true;
                offsetTopAndBottom((int) (y - mDownY));
                offsetLeftAndRight((int) (x - mDownX));
            }
            break;

        case MotionEvent.ACTION_UP:
            if (!isMove) {
                // 如果没有移动,则认为是点击
                if (mOnFloatClickListener != null) {
                    mOnFloatClickListener.onClick(v);
                }
            } else {
                // 如果移动了,则执行吸边逻辑
                if (getAdsorbType() == ADSORB_VERTICAL) {
                    adsorbTopAndBottom(event);
                } else if (getAdsorbType() == ADSORB_HORIZONTAL) {
                    adsorbLeftAndRight(event);
                }
            }

            if (getAdsorbTime() > 0) {
                mHandler.postDelayed(mRunnable, getAdsorbTime());
            }
            break;
    }
    return getIsCanDrag();
}`

原代码只有在贴吧的情况下会响应点击回调。
这样增加后,会在不贴边的情况下,响应点击回调。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant