android拖动一个imageview并放在另一个imageview上

时间:2017-01-22 18:55:15

标签: android drag-and-drop android-imageview ontouchlistener onlongclicklistener

我对整个Drag n Drop事情有点困惑。 我试图拖动我的应用程序的图标并将其放在图像中(用户应确定位置)。

我已经通过Drag and Drop阅读了google文档,但我已经设法让它部分工作,但我有一些问题。

我见过一些例子,作者在OnDragListener旁边实现了OnTouchListener和OnLongClickListener。

我是否必须实现这两个侦听器?

在另一个案例中,我注意到,OnDragListener和其他两个中的一个应用于同一个对象。

根据我的注意,我必须在将要处理将被拖放的对象的View上应用OnDragListener,我必须在其上实现OnTouchListener(或OnLongClickListener)。

我是对的吗?

现在,到我的项目:

我有这个布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/view_displayed"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/christmas"
        android:layout_centerHorizontal="true"
        android:id="@+id/imageViewCustomize"
        android:scaleType="fitCenter"
        android:padding="0dp"
        android:adjustViewBounds="true"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="0dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imagStamp"
        android:src="@drawable/ic_launcher"/>
</RelativeLayout>

第一个ImageView,保存从用户中选择的图像,在第二个ImageView上,我放置应用程序的图标。

直到现在,我有OnLongClickListener:

imageStamp.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);
                Customize.MyDragShadowBuilder myShadow = new Customize.MyDragShadowBuilder(imageStamp);

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

其中,MyDragShadowBuilder是一个内部类,取自google。

我的OnDragListener,是来自不同来源的混合(肯定有错误):

imageView.setOnDragListener(new View.OnDragListener() {
            @Override
            public boolean onDrag(View v, DragEvent event) {
                switch(event.getAction()) {
                    case DragEvent.ACTION_DRAG_STARTED:
                        layoutParamsStamp = (RelativeLayout.LayoutParams)v.getLayoutParams();
                        Log.d(msg, "Action is DragEvent.ACTION_DRAG_STARTED");

                        // Do nothing
                        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();
                        if(x_cord > imageView.getWidth()){
                            x_cord = imageView.getWidth();
                        }
                        else if(x_cord < 0){
                            x_cord = 0;
                        }
                        if(y_cord > imageView.getHeight()){
                            y_cord = imageView.getHeight();
                        }
                        else if(y_cord < 0){
                            y_cord = 0;
                        }
                        RelativeLayout.LayoutParams layoutParamsnew = (RelativeLayout.LayoutParams) v.getLayoutParams();
                        layoutParamsnew.leftMargin = x_cord - imageStamp.getWidth()/2;
                        layoutParamsnew.topMargin = y_cord - imageStamp.getHeight()/2;
                        imageStamp.setLayoutParams(layoutParamsnew);
                        imageStamp.setVisibility(View.VISIBLE);
                        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();
                        if(x_cord > imageView.getWidth()){
                            x_cord = imageView.getWidth();
                        }
                        else if(x_cord < 0){
                            x_cord = 0;
                        }
                        if(y_cord > imageView.getHeight()){
                            y_cord = imageView.getHeight();
                        }
                        else if(y_cord < 0){
                            y_cord = 0;
                        }
                        layoutParamsStamp.leftMargin = x_cord;
                        layoutParamsStamp.topMargin = y_cord;
                        v.setLayoutParams(layoutParamsStamp);
                        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();
                        if(x_cord > imageView.getWidth()){
                            x_cord = imageView.getWidth();
                        }
                        else if(x_cord < 0){
                            x_cord = 0;
                        }
                        if(y_cord > imageView.getHeight()){
                            y_cord = imageView.getHeight();
                        }
                        else if(y_cord < 0){
                            y_cord = 0;
                        }
                        break;

                    case DragEvent.ACTION_DRAG_ENDED   :
                        Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENDED");
                        imageStamp.setVisibility(View.VISIBLE);
                        // Do nothing
                        break;

                    case DragEvent.ACTION_DROP:
                        Log.d(msg, "ACTION_DROP event");
                        // Do nothing
                        break;
                    default: break;
                }
                return true;
            }
        });

对于以前的问题,任何帮助都会受到赞赏,尤其是答案。

提前致谢。

0 个答案:

没有答案