0
 public class Quiz extends Activity {

        int windowwidth;
        int windowheight;
        ImageView ima ima2;
        private View selected_item = null;
        private int offset_x = 0;
        private int offset_y = 0;

        private android.widget.LinearLayout.LayoutParams layoutParams1,
                layoutParams2;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.quiz);

            windowwidth = getWindowManager().getDefaultDisplay().getWidth();
            windowheight = getWindowManager().getDefaultDisplay().getHeight();

            System.out.println("width" + windowwidth);
            System.out.println("height" + windowheight);
   //on touch listener for image view 
            ima1 = (ImageView) findViewById(R.id.shuffledimage);
            ima1.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    layoutParams1 = (LinearLayout.LayoutParams) ima1
                            .getLayoutParams();
                    try {//action performed in image view
                        switch (event.getActionMasked()) {
                        case MotionEvent.ACTION_DOWN:
                            break;
                        case MotionEvent.ACTION_MOVE:
                            int x_cord = (int) event.getRawX();
                            int y_cord = (int) event.getRawY();
                            if (x_cord > windowwidth) {
                                x_cord = windowwidth;
                            }
                            if (y_cord > windowheight) {
                                y_cord = windowheight;
                            }
                            layoutParams1.leftMargin = x_cord - 25;
                            layoutParams1.topMargin = y_cord - 75;
                            ima1.setLayoutParams(layoutParams1);
                            break;

                        default:
                            break;
                        }
                    } catch (Exception e) {
                        // TODO: handle exception
                        e.getMessage();
                    }

                    return true;
                }
            });

            ima2 = (ImageView) findViewById(R.id.userimage);
            ima2.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    layoutParams2 = (LinearLayout.LayoutParams) ima2
                            .getLayoutParams();
                    try {
                        switch (event.getActionMasked()) {
                        case MotionEvent.ACTION_DOWN:
                            break;
                        case MotionEvent.ACTION_MOVE:
                            int x_cord = (int) event.getRawX();
                            int y_cord = (int) event.getRawY();
                            if (x_cord > windowwidth) {
                                x_cord = windowwidth;
                            }
                            if (y_cord > windowheight) {
                                y_cord = windowheight;
                            }
                            layoutParams2.leftMargin = x_cord - 25;
                            layoutParams2.topMargin = y_cord - 75;
                            ima2.setLayoutParams(layoutParams2);
                            break;
                        default:
                            break;

                        }

                    } catch (Exception e) {
                        e.getMessage();
                    }
                    return true;

                }

            });

        }

    }

//xmlファイル

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/ipodbackground"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="@drawable/headingmain1"
            android:gravity="center_horizontal" >

            <Button
                android:id="@+id/button1"
                android:layout_width="65dp"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:background="#00000000" />

            <Button
                android:id="@+id/button2"
                android:layout_width="40dp"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:background="#00000000" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:background="@drawable/quiztitle1" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="75dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp" >

            <LinearLayout
                android:id="@+id/l1"
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="25dp"
                android:background="@drawable/box"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/name"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="5dp"
                    android:text="TextView" />


                <ImageView
                    android:id="@+id/userimage"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="160dp"
                    android:layout_weight="1"
                    android:background="#FFFFFF"
                    android:src="@drawable/ic_launcher" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:layout_marginRight="5dp"
                android:layout_toRightOf="@+id/l1" >

                <ImageView
                    android:id="@+id/shuffledimage"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:src="@drawable/iconsearch" />
            </LinearLayout>
        </RelativeLayout>

    </LinearLayout>

//ある画像ビューから別の画像ビューに画像をドラッグアンドドロップしたいのですが、上記のコードセクションでは、ドラッグすると画像を正しくドラッグできますが、画像をソース画像ビューにドロップできません。誰でも解決策を教えてください

4

2 に答える 2

2

ドラッグ アンド ドロップについては、次のリンクを参照してください。

http://blahti.wordpress.com/2011/10/03/drag-drop-for-android-gridview/

http://www.twylah.com/dodulz/tweets/169426982533206016

于 2012-11-06T08:36:43.003 に答える
1

次の方法でそれを達成できます

public class DragAndDropBasicActivity extends Activity implements OnTouchListener {
    private ImageView letterView;                       // The letter that the user drags.
    private ImageView emptyLetterView;              // The letter outline that the user is supposed to drag letterView to.
    private AbsoluteLayout mainLayout;
    
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        mainLayout = (AbsoluteLayout) findViewById(R.id.mainLayout);
        mainLayout.setOnTouchListener(this);
        letterView = (ImageView) findViewById(R.id.letterView);
        letterView.setOnTouchListener(this);
    
        emptyLetterView = (ImageView) findViewById(R.id.emptyLetterView);
    }
    
    private boolean dragging = false;
    private Rect hitRect = new Rect();
    
    @Override
    /** NOTE:  Had significant problems when I tried to react to ACTION_MOVE on letterView.   Kept getting alternating (X,Y) 
     * locations of the motion events, which caused the letter to flicker and move back and forth.  The only solution I could 
     * find was to determine when the user had touched down on the letter, then process moves in the ACTION_MOVE 
     * associated with the mainLayout.
    */
    public boolean onTouch(View v, MotionEvent event) {
        boolean eventConsumed = true;
        int x = (int)event.getX();
        int y = (int)event.getY();
    
        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN) {
            if (v == letterView) {
                letterView.setImageResource(R.drawable.carkey1);
                dragging = true;
                eventConsumed = false;
            }
        } else if (action == MotionEvent.ACTION_UP) {
    
            if (dragging) {
                emptyLetterView.getHitRect(hitRect);
                if (hitRect.contains(x, y)) {
                    letterView.setImageResource(R.drawable.carkey3);
                    setSameAbsoluteLocation(letterView, emptyLetterView);
                }
            }
            dragging = false;
            eventConsumed = false;
    
        } else if (action == MotionEvent.ACTION_MOVE) {
            if (v != letterView) {
                if (dragging) {
                    setAbsoluteLocationCentered(letterView, x, y);
                }
            }
        }
    
        return eventConsumed;
    
    }
    
    
    private void setSameAbsoluteLocation(View v1, View v2) {
        AbsoluteLayout.LayoutParams alp2 = (AbsoluteLayout.LayoutParams) v2.getLayoutParams();
        setAbsoluteLocation(v1, alp2.x, alp2.y);
    }
    
    
    private void setAbsoluteLocationCentered(View v, int x, int y) {
        setAbsoluteLocation(v, x - v.getWidth() / 2, y - v.getHeight() / 2);
    }
    
    
    private void setAbsoluteLocation(View v, int x, int y) {
        AbsoluteLayout.LayoutParams alp = (AbsoluteLayout.LayoutParams) v.getLayoutParams();
        alp.x = x;
        alp.y = y;
        v.setLayoutParams(alp);
    }
}

xml

<xml version="1.0" encoding="utf-8"?>
    <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mainLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        
        
         <ImageView
            android:id="@+id/emptyLetterView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/carkey2" >
        </ImageView>
    
        <ImageView
            android:id="@+id/letterView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_x="200dp"
            android:layout_y="450dp"
            android:src="@drawable/carkey1" >
        </ImageView>
    
</AbsoluteLayout>
于 2013-06-28T06:18:46.937 に答える