将图像拖放到Android中的特定位置

时间:2012-01-16 09:48:21

标签: android drag-and-drop

我有一个问题,我在Android 2.2版本中使用Drag n Drop功能,我每次将图像拖放到另一个地方时都收到错误.......拖动工作正常但Drop返回IllegalArgumentException错误

错误堆栈:

01-16 20:27:46.029: E/AndroidRuntime(3931): FATAL EXCEPTION: main
01-16 20:27:46.029: E/AndroidRuntime(3931): java.lang.IllegalArgumentException: Given view not a child of com.SpeachTreat.common.DragLayer@44c95318
01-16 20:27:46.029: E/AndroidRuntime(3931):     at android.view.ViewGroup.updateViewLayout(ViewGroup.java:1876)
01-16 20:27:46.029: E/AndroidRuntime(3931):     at com.SpeachTreat.common.DragLayer.onDrop(DragLayer.java:90)
01-16 20:27:46.029: E/AndroidRuntime(3931):     at com.SpeachTreat.common.DragController.drop(DragController.java:446)
01-16 20:27:46.029: E/AndroidRuntime(3931):     at com.SpeachTreat.common.DragController.onTouchEvent(DragController.java:424)
01-16 20:27:46.029: E/AndroidRuntime(3931):     at com.SpeachTreat.common.DragLayer.onTouchEvent(DragLayer.java:63)
01-16 20:27:46.029: E/AndroidRuntime(3931):     at android.view.View.dispatchTouchEvent(View.java:3766)
01-16 20:27:46.029: E/AndroidRuntime(3931):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
01-16 20:27:46.029: E/AndroidRuntime(3931):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-16 20:27:46.029: E/AndroidRuntime(3931):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-16 20:27:46.029: E/AndroidRuntime(3931):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-16 20:27:46.029: E/AndroidRuntime(3931):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1676)
01-16 20:27:46.029: E/AndroidRuntime(3931):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1112)
01-16 20:27:46.029: E/AndroidRuntime(3931):     at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
01-16 20:27:46.029: E/AndroidRuntime(3931):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1660)
01-16 20:27:46.029: E/AndroidRuntime(3931):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
01-16 20:27:46.029: E/AndroidRuntime(3931):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 20:27:46.029: E/AndroidRuntime(3931):     at android.os.Looper.loop(Looper.java:123)
01-16 20:27:46.029: E/AndroidRuntime(3931):     at android.app.ActivityThread.main(ActivityThread.java:4627)
01-16 20:27:46.029: E/AndroidRuntime(3931):     at java.lang.reflect.Method.invokeNative(Native Method)
01-16 20:27:46.029: E/AndroidRuntime(3931):     at java.lang.reflect.Method.invoke(Method.java:521)
01-16 20:27:46.029: E/AndroidRuntime(3931):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-16 20:27:46.029: E/AndroidRuntime(3931):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-16 20:27:46.029: E/AndroidRuntime(3931):     at dalvik.system.NativeStart.main(Native Method)

XML:

<?xml version="1.0" encoding="utf-8"?>
<com.SpeachTreat.common.DragLayer
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher"
android:background="#ffffff"
    android:id="@+id/drag_layer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">


<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:layout_centerVertical="true"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" android:background="@drawable/header_back" android:orientation="vertical">

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/logo" />

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/header_text" />

        </LinearLayout>

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:layout_marginTop="10dip">

        <FrameLayout
            android:id="@+id/frameLayout1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" android:background="@drawable/start_playing_with_outline4">

            <ImageView
                android:id="@+id/img_Treat_Character"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/cute_animal_big3" android:layout_gravity="center_vertical|center_horizontal"/>

        </FrameLayout>

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:layout_marginTop="10dip">

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:orientation="vertical" android:background="@drawable/select_your_treat_with_outline">

             <LinearLayout
                android:id="@+id/linearLayout4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" android:layout_marginTop="10dip">

                <ImageView
                    android:id="@+id/img_treat1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/treat1" android:layout_marginLeft="30dip"/>
                <ImageView
                    android:id="@+id/img_treat2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/treat1" android:layout_marginLeft="10dip"/>
                <ImageView
                    android:id="@+id/img_treat3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/treat1" android:layout_marginLeft="10dip"/>
                <ImageView
                    android:id="@+id/img_treat4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/treat1" android:layout_marginLeft="10dip"/>
                <ImageView
                    android:id="@+id/img_treat5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/treat1" android:layout_marginLeft="10dip"/>

            </LinearLayout>
             <LinearLayout
                android:id="@+id/linearLayout5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" android:layout_marginTop="10dip">

                <ImageView
                    android:id="@+id/img_treat6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/treat1" android:layout_marginLeft="30dip"/>
                <ImageView
                    android:id="@+id/img_treat7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/treat1" android:layout_marginLeft="10dip"/>
                <ImageView
                    android:id="@+id/img_treat8"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/treat1" android:layout_marginLeft="10dip"/>
                <ImageView
                    android:id="@+id/img_treat9"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/treat1" android:layout_marginLeft="10dip"/>
                <ImageView
                    android:id="@+id/img_treat10"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/treat1" android:layout_marginLeft="10dip"/>

            </LinearLayout>

        </LinearLayout>

    </RelativeLayout>
 <RelativeLayout
        android:id="@+id/relativeLayout4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:layout_marginTop="10dip">

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="wrap_content"
         android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
        android:layout_height="wrap_content" android:layout_marginTop="10dip">

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/selectanewcharacter_button" />

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/end_game_button" android:layout_marginTop="10dip"/>


        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/green_button_with_shadow" android:layout_marginTop="20dip" android:layout_marginLeft="10dip"/>

        <ImageView
            android:id="@+id/imageView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/red_button_with_shadow" android:layout_marginTop="20dip" android:layout_marginLeft="10dip"/>

    </LinearLayout>
</RelativeLayout>
</LinearLayout>
</com.SpeachTreat.common.DragLayer>

DragLayer:

package com.blahti.example.drag;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

public class DragLayer extends MyAbsoluteLayout 
    implements DragSource, DropTarget
{
    DragController mDragController;

    /**
     * Used to create a new DragLayer from XML.
     *
     * @param context The application's context.
     * @param attrs The attribtues set containing the Workspace's customization values.
     */
    public DragLayer (Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setDragController(DragController controller) {
        mDragController = controller;
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        return mDragController.dispatchKeyEvent(event) || super.dispatchKeyEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return mDragController.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return mDragController.onTouchEvent(ev);
    }

    @Override
    public boolean dispatchUnhandledMove(View focused, int direction) {
        return mDragController.dispatchUnhandledMove(focused, direction);
    }

/**
 */
// DragSource interface methods

/**
 * setDragController
 *
 */

 /* setDragController is already defined. See above. */

/**
 * onDropCompleted
 *
 */

public void onDropCompleted (View target, boolean success)
{
    toast ("DragLayer2.onDropCompleted: " + target.getId () + " Check that the view moved.");
}

public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
        DragView dragView, Object dragInfo)
{
    View v = (View) dragInfo;
    toast ("DragLayer2.onDrop accepts view: " + v.getId ()
          + "x, y, xO, yO :" + new Integer (x) + ", " + new Integer (y) + ", "
          + new Integer (xOffset) + ", " + new Integer (yOffset));

    int w = v.getWidth ();
    int h = v.getHeight ();
    int left = x - xOffset;
    int top = y - yOffset;
    DragLayer.LayoutParams lp = new DragLayer.LayoutParams (w, h, left, top);
    this.updateViewLayout(v, lp);
}

public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
        DragView dragView, Object dragInfo)
{
}

public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
        DragView dragView, Object dragInfo)
{
}

public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
        DragView dragView, Object dragInfo)
{
}


public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
        DragView dragView, Object dragInfo)
{
    return true;
}


public Rect estimateDropLocation(DragSource source, int x, int y, int xOffset, int yOffset,
            DragView dragView, Object dragInfo, Rect recycle)
{
    return null;
}

public void toast (String msg)
{
    if (!DragActivity.Debugging) return;
    Toast.makeText (getContext (), msg, Toast.LENGTH_SHORT).show ();
} // end toast


} // end class

1 个答案:

答案 0 :(得分:1)

使用以下链接,它有很棒的Ui元素拖放教程

http://www.edumobile.org/android/android-beginner-tutorials/drag-and-drop-ui-element/