【问题标题】:How to avoid Android button dragging out side the screen如何避免Android按钮拖出屏幕
【发布时间】:2014-02-20 10:16:32
【问题描述】:

我已经在我的应用程序中实现了按钮拖动,但它的工作问题是为什么拖动按钮会从侧面向外移动而无法看到按钮。但修复它。我需要将屏幕拖到一边。我已经尝试了下面的代码,如果有任何错误请纠正我,

@Override
    public boolean onTouch(View v, MotionEvent event) 
    {
            final int d_X = (int) event.getRawX();
            final int d_Y = (int) event.getRawY();
            PointF start = new PointF();

            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    start.set(event.getX(),event.getY());
                    if(shouldAllowToDrag) // if it true then i will allow to drag.
                    {
                        RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
                        _xDelta = d_X - lParams.leftMargin;
                        _yDelta = d_Y - lParams.topMargin;
                    }
                    else 
                    {
                        //check for double click
                        long pressTime = System.currentTimeMillis();
                        // If double click...
                        String buttonTitle = moveButton.getText().toString();
                        if (pressTime - lastPressTime <= DOUBLE_PRESS_INTERVAL) {
                            Log.i(TAG,"doubleTappedistrue");

                            hasLatchedDown = false;
                            if(buttonTitle.equals("Unlocked"))
                            {
                                hasButtonUnlocked = false;
                                moveButton.setText("Locked");
                            }
                            else if(buttonTitle.equals("Locked"))
                            {
                                hasButtonUnlocked = true;
                                moveButton.setText("Unlocked");
                            }
                        }
                        else
                        {
                            hasLatchedDown = true;
                            buttonLastStateText = buttonTitle;
                            moveButton.setText("Unlocked");
                        }
                        lastPressTime = pressTime;  

                    }
                    break;
                case MotionEvent.ACTION_UP:
                    if(!shouldAllowToDrag)
                    {
                        if(hasLatchedDown)
                        {
                            hasLatchedDown = false;
                            moveButton.setText(buttonLastStateText);
                        }
                    }
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    if(shouldAllowToDrag){
                        View parentView = (View) v.getParent();
                        parentView.getWidth();
                        parentView.getHeight();
                        imageView.getWidth();
                        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
                        layoutParams.leftMargin = d_X - _xDelta;
                        layoutParams.topMargin = d_Y - _yDelta;

                    v.setLayoutParams(layoutParams);
                    }




                    break;
            }
            return true;
    }

【问题讨论】:

    标签: android drag-and-drop touch android-button android-event


    【解决方案1】:

    您应该添加一个最小/最大检查:

    layoutParams.leftMargin = Math.min(Math.max(d_X - _xDelta,0), parentView.getWidth() - layoutParams.width);
    

    topMargin 的想法相同。 (代码未编译检查) 让我知道这是否有帮助。

    【讨论】:

    • topMargin 正在工作,你能帮我看看右下角吗
    猜你喜欢
    • 1970-01-01
    • 2014-01-22
    • 2013-05-02
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 1970-01-01
    相关资源
    最近更新 更多