欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android自定义随手指移动Button

程序员文章站 2022-06-09 10:28:34
...

定义随手指滑动按钮,限制按钮不可滑动超出屏幕。解决与点击事件冲突问题.

public class MoveButton extends AppCompatButton {

    private int lastX;
    private int lastY;
    private int screenWidth, screenHeight;

    public MoveButton(Context context) {
        super(context);
    }

    public MoveButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        screenWidth = displayMetrics.widthPixels;
        screenHeight = displayMetrics.heightPixels;
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }



    private int firstX;
    private int firstY;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: // 按下
                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();
                firstX = (int) event.getRawX();
                firstY = (int) event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                int dx = (int) (event.getRawX() - lastX);
                int dy = (int) (event.getRawY() - lastY);
                int left = getLeft() + dx;
                int top = getTop() + dy;
                int right = getRight() + dx;
                int bottom = getBottom() + dy;
                if (left < 0) {
                    left = 0;
                    right = left + getWidth();
                }
                if (right > screenWidth) {
                    right = screenWidth;
                    left = right - getWidth();
                }
                if (top < 0) {
                    top = 0;
                    bottom = top + getHeight();
                }
                if (bottom > screenHeight) {
                    bottom = screenHeight;
                    top = bottom - getHeight();
                }
                layout(left, top, right, bottom);
                Log.v("TAG", "left=" + left + "top" + top + "right" + right + "bottom" + bottom);
                Log.v("TAG", "screenWidth=" + screenWidth + "screenHeight=" + screenHeight);
                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();
                return true;
            case MotionEvent.ACTION_UP:
                int moveX = (int) (event.getRawX() - firstX);
                int moveY = (int) (event.getRawY() - firstY);
                if (moveX > 4 || moveX < -4 || moveY > 4 || moveY < -4) {
                    return true;
                }
                if (listener != null) {
                    listener.onClick(this);
                }
                break;
            default:
                break;
        }

        return true;
    }
     /**
     * 点击事件监听,解决滑动与点击冲突
     *
     * @param listener
     */
    public void setMoveOnClickListener(OnClickListener listener) {
        this.listener = listener;
        setOnClickListener(listener);
    }

    /**
     * 屏蔽点击事件
     *
     * @param l
     */
    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
        super.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.v("MoveButton", "不准用这个方法!!!");
            }
        });
    }

**用法:在布局文件

`moveButton = view.findViewById(R.id.btn_play_video);
  moveButton.setMoveOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

          }
        });