Imageview不会在掉落事件中掉线

时间:2014-02-25 06:45:14

标签: android drag-and-drop ontouchlistener

我正在尝试onDrag事件移动ImageView。我的ImageView移动得非常好,但问题是ImageViewDROP事件的最后位置没有显示可见。 以下是我的代码......我在哪里弄错了?

package com.example.dragndropdemo;

import android.os.Bundle;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipDescription;
import android.graphics.Canvas;
import android.graphics.Point;
import android.util.Log;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.widget.*;

public class MainActivity extends Activity{
   ImageView ima;
   private static final String IMAGEVIEW_TAG = "Android Logo";
   String msg;

   private android.widget.RelativeLayout.LayoutParams layoutParams;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      ima = (ImageView)findViewById(R.id.iv_logo);
      // Sets the tag
      ima.setTag(IMAGEVIEW_TAG);

      ima.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        ClipData.Item item = new ClipData.Item((CharSequence)v.getTag());

                    String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
                    ClipData dragData = new ClipData(v.getTag().toString(), mimeTypes, item);
                    // Instantiates the drag shadow builder.
                    DragShadow dragShadow=new DragShadow(v);

                    // Starts the drag
                    v.startDrag(dragData    // the data to be dragged
                            , dragShadow    // the drag shadow builder
                            , v             // no need to use local data
                            , 0             // flags (not currently used, set to 0)
                            );
                    v.setVisibility(View.INVISIBLE);
                    return true;
            }
            else{
                return false;
            }
         }
      });

      // Create and set the drag event listener for the View
      ima.setOnDragListener( new OnDragListener(){
         @Override
         public boolean onDrag(View v,  DragEvent event){
             int dragEvent=event.getAction();
         switch(event.getAction())                   
         {
            case DragEvent.ACTION_DRAG_STARTED:
               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");
               break;

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

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

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

            case DragEvent.ACTION_DROP:
               Log.d(msg, "ACTION_DROP event");
               ImageView imgvw=(ImageView)event.getLocalState();
               imgvw.setVisibility(View.VISIBLE);
               break;

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

   private class DragShadow extends View.DragShadowBuilder
   {

    public DragShadow(View view) {
        super(view);
    }

    @Override
    public void onDrawShadow(Canvas canvas) {
        super.onDrawShadow(canvas);
    }

    @Override
    public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
        View v= getView();

        int height=v.getHeight();
        int width=v.getWidth();

        shadowSize.set(width, height);
        shadowTouchPoint.set((int)width/2,(int)height/2);
    }

   }
}

2 个答案:

答案 0 :(得分:1)

我几个小时以来一直在努力解决同样的问题。但我在ApiDemos中找到了一个解决方案。您可以查看包com.example.android.apis.view内部,然后您会看到DragAndDropDemo.java

在里面,您会注意到可拖动对象是DraggableDot的实例,它们也是丢弃点。然后查看DraggableDot内部,它只是实现View的{​​{1}}的子类。

最后,我创建了onDragEvent的子类并插入了View方法。我使用这个子类作为我的丢弃点,令人惊讶的是,我的onDragEvent开始注册其他DragListener操作,尤其是DragEvent

答案 1 :(得分:-1)

更改:

ImageView imgvw=(ImageView)event.getLocalState();

imgvw.setVisibility(View.VISIBLE);

作为

v.setVisibility(View.VISIBLE);