拖放后移动到原来的位置

拖放后移动到原来的位置

本文介绍了拖放后移动到原来的位置,放下目标与阴影在Android中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

onDrag类:

private class ChoiceDragListener implements OnDragListener {

    @Override
    public boolean onDrag(View v, DragEvent event) {
        switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            // no action necessary
            break;
        case DragEvent.ACTION_DRAG_ENTERED:
            // no action necessary
            break;
        case DragEvent.ACTION_DRAG_EXITED:
            // no action necessary
            break;
        case DragEvent.ACTION_DROP:
            // handle the dragged view being dropped over a drop view
            View view = (View) event.getLocalState();

            dropTarget = (RelativeLayout) v;

            dropped = (RelativeLayout) view;
            tagDropped = dropped.getTag().toString();

            Log.i("tagDropped", "" + tagDropped);
            tagDropTarget = dropTarget.getTag().toString();
            Log.i("tagDropTarget", "" + tagDropTarget);

            matchTag();

            break;
        case DragEvent.ACTION_DRAG_ENDED:
            // no action necessary
            break;
        default:
            break;
        }
        return true;
    }
}

ChoiceTouchListener类:

ChoiceTouchListener class:

private final class ChoiceTouchListener implements OnTouchListener {
    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
             */
            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
                    view);
            // start dragging the item touched
            view.startDrag(data, shadowBuilder, view, 0);
            return true;
        } else {
            return false;
        }
    }
}

现在我想移动对象在原来的位置被拖动

Now i want to move object on its original position where it is drag

我有这样的搜索,但没有回答任何一个

I have search like that but not answered any one drag and drop with onDraglistener animate to go back to original position if not dropped on target

推荐答案

首先得到拖动器控件和拖动接收器控件之间的距离:
textview1 是拖动器控件
textview2 是拖动接收器控件

first get the distance between dragger control and drag receiver control:textview1 is dragger controltextview2 is drag receiver control

//if text view2 is below textview one and text view 2 is on the right of text view one
int topMargin=textview2_top_margin - textview1_top_margin
int leftMargin=textview2_left_margin - textview1_left_margin

public void animation() {
  Animation animation = new TranslateAnimation(left_margin, 0, topMargin, 0);
  animation.setDuration(1000);
  controlToAnimate.startAnimation(animation);
 }

这篇关于拖放后移动到原来的位置,放下目标与阴影在Android中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!