如何在相对布局中定义可放置区域

时间:2014-02-27 19:18:56

标签: android drag-and-drop

我创建了一个简单的应用程序来拖放相对布局的图像。一切都很好,但我不能把我的形象放在我的布局中。实际上,在我的拖拽监听器中,除了图像附近的区域外,我得到DragEvent.ACTION_DRAG_EXITED。我使用拖放ApI来实现这种方法。任何解决方案 谢谢 主要活动:

public class MainActivity extends Activity {

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

    display = getWindowManager().getDefaultDisplay();
    //RelativeLayout rl = (RelativeLayout) findViewById(R.id.Content);

    ImageView imv1 = (ImageView) findViewById(R.id.imageview1);
    imv1.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            ClipData dragImage = ClipData.newPlainText("", "");

            //inja dragshadowbuildere default ro estefade kardam , on mydrag... vase sakhtane customizeshe
            View.DragShadowBuilder myShadow = new DragShadowBuilder(v); //in imageview bod gir dad v kardamesh bebin gir nade
            v.startDrag(dragImage, myShadow, null, 0);
            return false;
        }
    });

    //ImageView IV = new ImageView(this);
    myDragEventListener mDragListen = new myDragEventListener();
    imv1.setOnDragListener(mDragListen);
       }
   }

这是拖拽监听器:

public class myDragEventListener implements OnDragListener{

// This is the method that the system calls when it dispatches a drag event to the
// listener.
@Override
public boolean onDrag(View v, DragEvent event) {
    // Defines a variable to store the action type for the incoming event
    final String tag = "OnDragListener";
    final int action = event.getAction();
    switch (action) {
    case DragEvent.ACTION_DRAG_EXITED:
                 Log.v("Ondrag","On exited");
    case DragEvent.ACTION_DRAG_STARTED:
        // Determines if this View can accept the dragged data
        if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {

            // As an example of what your application might do,
            // applies a blue color tint to the View to indicate that it can accept
            // data.
            ((ImageView) v).setColorFilter(Color.BLUE);

            // Invalidate the view to force a redraw in the new tint
            v.invalidate();

            Log.v(tag, "onStart");
            // returns true to indicate that the View can accept the dragged data.
            return(true);

            } else {

            // Returns false. During the current drag and drop operation, this View will
            // not receive events again until ACTION_DRAG_ENDED is sent.
            return(false);

            }
    case DragEvent.ACTION_DRAG_ENTERED :
        //Log.v("onDrag", "ENTERED");
        ((ImageView) v).setColorFilter(Color.GREEN);
        Log.v(tag, "onEnter");

    // Invalidate the view to force a redraw in the new tint
        v.invalidate();

        return(true);
    case DragEvent.ACTION_DRAG_LOCATION:
        Log.v(tag, "onLocation");

        // Ignore the event
            return(true);
    case DragEvent.ACTION_DROP:

        Log.v(tag, "onDrop");

        // Gets the item containing the dragged data
        ClipData.Item item = event.getClipData().getItemAt(0);

        // Turns off any color tints

        //MarginLayoutParams marginParams = new MarginLayoutParams(v.getLayoutParams());
        RelativeLayout.LayoutParams marginParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
        int left = (int) event.getX() - (v.getWidth() / 2);
        int top = (int) event.getY() - (v.getHeight() / 2);
        //marginParams.setMargins(left, top, 0, 0);
        if(left > MainActivity.display.getWidth())
            left = MainActivity.display.getWidth();
        if(top > MainActivity.display.getHeight()){
            top = MainActivity.display.getHeight();
        }
        marginParams.leftMargin = left;
        marginParams.topMargin = top ;
        //RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(marginParams);
        v.setLayoutParams(marginParams);

        // Invalidates the view to force a redraw
        v.invalidate();

        // Returns true. DragEvent.getResult() will return true.
        return(true);
    case DragEvent.ACTION_DRAG_ENDED:

        // Turns off any color tinting
        ((ImageView) v).clearColorFilter();

        // Invalidates the view to force a redraw
        v.invalidate();

        // Does a getResult(), and displays what happened.
        if (event.getResult()) {
            //Toast.makeText(this, "The drop was handled.", Toast.LENGTH_LONG);
            Log.v("Ondrag", "ok");

        } else {
            //Toast.makeText(this, "The drop didn't work.", Toast.LENGTH_LONG);
            Log.v("Ondrag", "NoOk");
        };

        // returns true; the value is ignored.
        return(true);
    default:
        Log.v("Ondrag", "default");
        break;
    }
    return false;
}

} 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/Content"
android:background="@android:color/background_dark">

<ImageView
    android:id="@+id/imageview1"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginLeft="40dp"
    android:layout_marginTop="81dp"
    android:src="@drawable/ic_launcher" />

0 个答案:

没有答案