当拖动时,Android ImageView 消失了

3
我希望能够在我的RelativeLayout中随意拖动ImageView。当我尝试拖动ImageView时,它可以移动,但是当我从屏幕上释放手指时,它会消失。我找不到错误所在。
以下是我的代码结构:
ImageView的OnLongClickListener
imageView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            ClipData.Item item = new ClipData.Item((CharSequence)v.getTag());
            String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};

            ClipData dragData = new ClipData(v.getTag().toString(),mimeTypes, item);
            View.DragShadowBuilder myShadow = new View.DragShadowBuilder(imageView);

            v.startDrag(dragData,myShadow,null,0);
            v.setVisibility(View.GONE);
            return true;
        }
    });

DragListener Method

        imageView.setOnDragListener(new View.OnDragListener() {

        @Override
        public boolean onDrag(View v, DragEvent event) {
            switch (event.getAction()) {
                case DragEvent.ACTION_DRAG_STARTED:

                     layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams(); //layoutParams is defined on Top.
                    Log.d(msg, "Action is DragEvent.ACTION_DRAG_STARTED");

                   break;

                case DragEvent.ACTION_DRAG_ENTERED:
                    Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENTERED");
                    int x_cord = (int) event.getX();
                    int y_cord = (int) event.getY();
                    Log.d(msg,"Drag Location Entered "+x_cord+" Ycord "+y_cord);

                    break;


                case DragEvent.ACTION_DRAG_EXITED:
                    Log.d(msg, "Action is DragEvent.ACTION_DRAG_EXITED");
                    x_cord = (int) event.getX();
                    y_cord = (int) event.getY();
                    Log.d(msg, "Drag Exited xcord " + x_cord + " Ycord " + y_cord);


                    layoutParams.leftMargin = x_cord;
                    layoutParams.topMargin = y_cord;

                    v.setLayoutParams(layoutParams);
                    Log.d(msg, "layoutparams set success.");
                        v.setVisibility(View.VISIBLE);
                        Log.d(msg,"Made View Visible");

                    break;

                case DragEvent.ACTION_DRAG_LOCATION:
                    Log.d(msg, "Action is DragEvent.ACTION_DRAG_LOCATION");
                    x_cord = (int) event.getX();
                    y_cord = (int) event.getY();
                    Log.d(msg,"Drag Location xcord "+x_cord+" Ycord "+y_cord);
                    break;


                case DragEvent.ACTION_DRAG_ENDED:
                    Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENDED");
                    break;

                case DragEvent.ACTION_DROP:
                    Log.d(msg, "ACTION_DROP event");


                    break;
                default:
                    break;
            }
            return true;
        }
    });

OnTouchListener Method

        imageView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                ClipData data = ClipData.newPlainText("", "");
                View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(imageView);
                imageView.startDrag(data, shadowBuilder, imageView, 0);
                return true;
            }
            else
            {
                return false;
            }
        }
    });

一个技巧是将颜色过滤器应用于具有屏幕背景颜色的图像(请参阅链接:http://developer.android.com/guide/topics/ui/drag-drop.html),并在onLongClickListener中注释掉`v.setVisibility(View.GONE);`行。 - Prasad
1个回答

0
我怀疑你整个事情都搞错了。在底部行中,你看不到ImageView是因为你调用了:
v.setVisibility(View.GONE);

在你的OnCLickListener中,你从未将其更改回来。

问题是你将DragListener分配给布局中要通知有关DragEvents的任何View,但即使您不将任何侦听器分配给任何View,只需调用startDrag()系统就开始使用ShadowBuilder进行拖动。

因此,总结一下,如果您拖动一个View,则通常会将DragListener分配给布局中的其他Views,这些Views是您悬停在其上方的视图。看起来您认为通过调用以下内容:

v.setLayoutParams(layoutParams);

这就是导致 View 跟随手指移动的原因,实际上是调用了:

v.startDrag()

这是它的责任。

无论如何,如果你调用:

 case DragEvent.ACTION_DRAG_ENDED:
        case DragEvent.ACTION_DRAG_ENDED:
        v.setVisibility(View.VISIBLE);
        break;

你应该看到ImageView的背面。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接