视图的位置在拖动侦听器中更改

时间:2015-04-06 12:23:37

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

我在父布局上放置视图。我想在长按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;
    }

1 个答案:

答案 0 :(得分:0)

您可以使用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