java - 在 android 中有阴影的目标上拖放后拖放回到原来的位置?

标签 java android drag-and-drop

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 类:

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;
        }
    }
}

现在我想将对象移动到它被拖动的原始位置

我有这样的搜索但没有回答任何人drag and drop with onDraglistener animate to go back to original position if not dropped on target

最佳答案

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

//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);
 }

关于java - 在 android 中有阴影的目标上拖放后拖放回到原来的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20741022/

相关文章:

javascript - (我的)拖放 (javascript) 无法正确处理文本

firefox - 是否有解决方法可以使用 Firefox 和 HTML 5 拖放输入类型?

java - 对于 i0 变量

java - Eclipse AVD 给出错误 'Unfortunately, (app) has stopped'

java - 检查arraylist中是否存在具有特定坐标的圆?

Java,拖放更改面板顺序

java - 我正在尝试读取一个文本文件,然后拆分行,以便获得两组不同的名称

android - 如何在android中使用设备管理员应用程序阻止安装其他应用程序

android - 将位图添加到 Android 中的 View

java - 按日期分组,看看日期是不是今天?