【问题标题】:Position of view changes in on drag listner拖动侦听器上视图的位置更改
【发布时间】:2015-04-06 12:23:37
【问题描述】:

我在父布局上放置视图。我想通过长按 L1 来拖动整个视图。当我长按 L1 时,视图进入拖动模式,但将触摸位置更改为视图中心 (x,y)。 我想按住布局(L1)的上栏拖动视图。

View:
    ______________
   |______L1______|
   |              |
y  |      L2      |
   |              |
   |______________|
           x


    @Override
    public boolean onDrag(View v, DragEvent event) {

        // Handles each of the expected events
        switch (event.getAction()) {

        //signal for the start of a drag and drop operation.
        case DragEvent.ACTION_DRAG_STARTED:
            // do nothing

            break;

        //the drag point has entered the bounding box of the View
        case DragEvent.ACTION_DRAG_ENTERED:
            v.setBackground(targetShape);   //change the shape of the view
            break;

        //the user has moved the drag shadow outside the bounding box of the View
        case DragEvent.ACTION_DRAG_EXITED:
            v.setBackground(normalShape);   //change the shape of the view back to normal
            break;

        //drag shadow has been released,the drag point is within the bounding box of the View
        case DragEvent.ACTION_DROP:
             doStuff();
              break;

        //the drag and drop operation has concluded.
        case DragEvent.ACTION_DRAG_ENDED:
            v.setBackground(normalShape);   //go back to normal shape

        default:
            break;
        }
        return true;
    }

【问题讨论】:

    标签: android view drag-and-drop position touch


    【解决方案1】:

    您可以使用View.DragShadowBuilder 设置图像中的拖动点:

    view.onProvideShadowMetrics(/* size of shadow */,
    /* provide Point object with x cordinate equal to whole
     width of L1 and y cordinate equal to height of L1*/);
    

    这样您就可以将阴影触摸点设置为 L1

    可以参考Developer page for DragShadowBuilder

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多