10

我正在制作一个游戏应用程序,我需要在onDraglistener这里使用我在实现的类上进行的拖放操作onDraglistener

  @Override
public boolean onDrag(View v, DragEvent event) {
    switch (event.getAction()) {
    case DragEvent.ACTION_DRAG_STARTED:
        Log.v("here","drag started");
        //no action necessary
        break;
    case DragEvent.ACTION_DRAG_ENTERED:
        //no action necessary
        Log.v("here","drag enteres");
        break;
    case DragEvent.ACTION_DRAG_LOCATION:
     int mCurX = (int) event.getX();
    int mCurY = (int) event.getY();
    Log.v("Cur(X, Y) : " ,"here ::" + mCurX + ", " + mCurY );
    break;
    case DragEvent.ACTION_DRAG_EXITED:        
        //no action necessary

        Log.v("here","drag exits");
        break;
    case DragEvent.ACTION_DROP:
            .................do what ever when dropped...
     }

在这里,我正在对touchlistener要拖放的视图做什么:

public boolean onTouch(View view, MotionEvent motionEvent) {
    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        /*
         * Drag details: we only need default behavior
         * - clip data could be set to pass data as part of drag
         * - shadow can be tailored
         */
        view.setTag("option"+index);
        ClipData data = ClipData.newPlainText("tag", "option"+index);
        shadowBuilder = new View.DragShadowBuilder(view);

        //start dragging the item touched
        view.startDrag(data, shadowBuilder, view, 0);


        offsetX = (int)view.getX();//(int)motionEvent.getX();
         offsetY = (int)view.getY();//motionEvent.getY();
         Log.v("here","it is ::" + (int)motionEvent.getX() + " , "+(int)motionEvent.getY());

         return false;
 }

现在它构建了阴影生成器,它可以拖动到您拖动手指的任何位置。

当没有下降到目标时,如何为阴影构建器或视图设置动画返回其位置?

我尝试public boolean onInterceptTouchEvent(MotionEvent event)移动到父视图并获取 x,y 位置并为其设置动画以返回其原始位置,但它被取消了操作。

有什么方法可以将 x,y 坐标获取到视图放置在目标之外的位置。然后我就可以在位置之间对其进行动画处理。

4

1 回答 1

-3

您需要处理下降:

switch (event.getAction()) {
    case DragEvent.ACTION_DRAG_ENTERED:
     //break here was removed in purpose so you can see the shadow and the element is still at its initial place.
    case DragEvent.ACTION_DROP:
            View view = (View) event.getLocalState();
        if (prioritiesList.contains(v)) {

            ViewGroup viewgroup = (ViewGroup) view.getParent();
            viewgroup.removeView(view);

            LinearLayout containView = (LinearLayout) v;
            containView.addView(view);
            view.setVisibility(View.VISIBLE);
                    break;
}
于 2014-04-30T15:28:07.450 回答