0

相対レイアウトでオブジェクト(星、ハート)をドラッグする必要があるドラッグアンドドロップAndroidアプリケーションを作成しています。

私の問題は、オブジェクトをドラッグするたびにどこにでも行くことです。

これは私のコードです:

public class MainActivity extends Activity {    
int _xDelta;
int _yDelta;
  @Override
  public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener());
findViewById(R.id.myimage2).setOnTouchListener(new MyTouchListener());
findViewById(R.id.myimage3).setOnTouchListener(new MyTouchListener());

findViewById(R.id.left).setOnDragListener(new MyDragListener());
findViewById(R.id.right).setOnDragListener(new MyDragListener());

  }

これは私のタッチリスナーです:

private final class MyTouchListener implements OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {

       final int X = (int) motionEvent.getRawX();
        final int Y = (int) motionEvent.getRawY();
        switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                LinearLayout.LayoutParams lParams = (LinearLayout.LayoutParams) view.getLayoutParams();
                _xDelta = X - lParams.leftMargin;
                _yDelta = Y - lParams.topMargin;

                ClipData data = ClipData.newPlainText("", "");
                DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
                view.startDrag(data, shadowBuilder, view, 0);
                view.setVisibility(View.INVISIBLE);

                break;
            case MotionEvent.ACTION_UP:
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                break;
            case MotionEvent.ACTION_POINTER_UP:
                break;
            case MotionEvent.ACTION_MOVE:
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
                layoutParams.leftMargin = X - _xDelta;
                layoutParams.topMargin = Y - _yDelta;
                layoutParams.rightMargin = -250;
                layoutParams.bottomMargin = -250;
                view.setLayoutParams(layoutParams);

                break;
        }
        return true;
}
  }

そして、これは私のドラッグリスナーです:

class MyDragListener implements OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
  int action = event.getAction();
  switch (event.getAction()) {
  case DragEvent.ACTION_DRAG_STARTED:
    break;
  case DragEvent.ACTION_DRAG_ENTERED:
    break;
  case DragEvent.ACTION_DRAG_EXITED:
    break;
  case DragEvent.ACTION_DROP:
    // Dropped, reassign View to ViewGroup
    View view = (View) event.getLocalState();
    ViewGroup owner = (ViewGroup) view.getParent();
    owner.removeView(view);
    LinearLayout container = (LinearLayout) v;
    container.addView(view);
    view.setVisibility(View.VISIBLE);
    break;
  case DragEvent.ACTION_DRAG_ENDED:
    default:
    break;
  }
  return true;
}
  }

これは私のレイアウトです:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:columnWidth="300dp"
android:orientation="vertical"
android:rowCount="2"
android:stretchMode="columnWidth" >

<LinearLayout
    android:id="@+id/topleft"
    android:layout_width="478dp"
    android:layout_height="200dp"
    android:layout_column="0"
    android:layout_row="0"
    android:background="@drawable/shape" 
    android:orientation="vertical"
    >

</LinearLayout>

<LinearLayout
    android:id="@+id/topright"
    android:layout_width="wrap_content"
    android:layout_height="200dp"
    android:layout_column="1"
    android:layout_gravity="left"
    android:layout_row="0"
    android:background="@drawable/shape"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/myimage2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="0"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/myimage1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="0"
        android:src="@drawable/ic_launcher" />

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

あなたが私を助けてくれることを願っています!ありがとう

4

1 に答える 1

0

コードを更新しました。申し訳ありませんが、すべてめちゃくちゃですが、それでも画像を移動できます。私は画像 1 のみに取り組みましたが、他の画像も同様に作業できます。

主な活動

import android.app.Activity;
import android.content.ClipData;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.LinearLayout;

public class ActivityMain extends Activity {
    int _xDelta;
    int _yDelta;
    int dropZone1_X, dropZone2_X, dropZone3_X, dropZone1_Y, dropZone2_Y,
            dropZone3_Y, movingCoordinateLeft = 0, movingCoordinateTop = 0;
    int leftMargin, topMargin, bottomMargin, rightMargin;

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

        findViewById(R.id.myimage1).setOnTouchListener(dragt);
        findViewById(R.id.myimage2).setOnTouchListener(new MyTouchListener());
        findViewById(R.id.myimage3).setOnTouchListener(new MyTouchListener());

        // findViewById(R.id.left).setOnDragListener(new MyDragListener());
        // findViewById(R.id.right).setOnDragListener(new MyDragListener());

    }

    private final class MyTouchListener implements OnTouchListener {
        public boolean onTouch(View view, MotionEvent motionEvent) {

            final int X = (int) motionEvent.getRawX();
            final int Y = (int) motionEvent.getRawY();
            switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:

                // LinearLayout.LayoutParams lParams =
                // (LinearLayout.LayoutParams) view
                // .getLayoutParams();

                FrameLayout.LayoutParams lParams = (LayoutParams) view
                        .getLayoutParams();

                _xDelta = X - lParams.leftMargin;
                _yDelta = Y - lParams.topMargin;

                ClipData data = ClipData.newPlainText("", "");
                DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
                        view);
                view.startDrag(data, shadowBuilder, view, 0);
                view.setVisibility(View.INVISIBLE);

                break;
            case MotionEvent.ACTION_UP:

                // code to set to a position

                LinearLayout.LayoutParams dropLayoutParams = (LinearLayout.LayoutParams) view
                        .getLayoutParams();
                dropLayoutParams.leftMargin = leftMargin;
                dropLayoutParams.topMargin = topMargin;
                dropLayoutParams.rightMargin = topMargin;
                dropLayoutParams.bottomMargin = bottomMargin;
                view.setLayoutParams(dropLayoutParams);
                view.setVisibility(View.VISIBLE);
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                break;
            case MotionEvent.ACTION_POINTER_UP:
                break;
            case MotionEvent.ACTION_MOVE:
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view
                        .getLayoutParams();
                layoutParams.leftMargin = X - _xDelta;
                leftMargin = layoutParams.leftMargin;

                layoutParams.topMargin = Y - _yDelta;
                topMargin = layoutParams.topMargin;

                layoutParams.rightMargin = -250;
                rightMargin = layoutParams.rightMargin;
                layoutParams.bottomMargin = -250;
                bottomMargin = layoutParams.bottomMargin;
                view.setLayoutParams(layoutParams);

                break;
            }
            return true;
        }
    }

    class MyDragListener implements OnDragListener {
        @Override
        public boolean onDrag(View v, DragEvent event) {
            int action = event.getAction();
            switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
                break;
            case DragEvent.ACTION_DRAG_ENTERED:
                break;
            case DragEvent.ACTION_DRAG_EXITED:
                break;
            case DragEvent.ACTION_DROP:
                // Dropped, reassign View to ViewGroup
                View view = (View) event.getLocalState();
                ViewGroup owner = (ViewGroup) view.getParent();
                owner.removeView(view);
                LinearLayout container = (LinearLayout) v;
                container.addView(view);
                view.setVisibility(View.VISIBLE);
                break;
            case DragEvent.ACTION_DRAG_ENDED:
            default:
                break;
            }
            return true;
        }
    }

    // onCreate
    OnTouchListener dragt = new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            FrameLayout.LayoutParams par = (LayoutParams) v.getLayoutParams();
            switch (v.getId()) {// What is being touched
            /***
             * 
             * Answer option 1
             * 
             * ***/
            case R.id.myimage1: {
                // Which action is being taken
                switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE: {
                    par.topMargin = (int) event.getRawY()
                            - (v.getHeight() + 22);
                    par.leftMargin = (int) event.getRawX()
                            - (v.getWidth() / 2 + 350);

                    movingCoordinateLeft = (int) event.getRawX()
                            - (v.getWidth() / 2 + 0);
                    movingCoordinateTop = par.topMargin;

                    System.out.println("Answer 1 --- left"
                            + movingCoordinateLeft + "---top"
                            + movingCoordinateTop);

                    v.setLayoutParams(par);

                    break;
                }// inner case MOVE
                case MotionEvent.ACTION_UP: {
                    break;
                }// inner case UP
                case MotionEvent.ACTION_DOWN: {

                    System.out.println("left" + event.getRawX());
                    System.out.println("top" + event.getRawY());

                    break;
                }// inner case UP
                }// inner switch
                break;
            }// case pawn

            /***
             * 
             * Answer option 2
             * 
             * ***/

            case R.id.myimage2: {// Which action is being taken
                switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE: {

                    par.topMargin = (int) event.getRawY()
                            - (v.getHeight() + 22);
                    par.leftMargin = (int) event.getRawX()
                            - (v.getWidth() / 2 + 150);

                    movingCoordinateLeft = (int) event.getRawX()
                            - (v.getWidth() / 2 + 0);
                    movingCoordinateTop = par.topMargin;

                    v.setLayoutParams(par);

                    break;
                }// inner case MOVE
                case MotionEvent.ACTION_UP: {

                    break;
                }// inner case UP
                case MotionEvent.ACTION_DOWN: {

                    break;
                }// inner case UP
                }// inner switch
                break;
            }// case pawn2

            /***
             * 
             * Answer option 3
             * 
             * ***/

            case R.id.myimage3: {// Which action is being taken
                switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE: {

                    par.topMargin = (int) event.getRawY()
                            - (v.getHeight() + 22);
                    par.leftMargin = (int) event.getRawX()
                            - (v.getWidth() / 2 + 150);

                    movingCoordinateLeft = (int) event.getRawX()
                            - (v.getWidth() / 2 + 0);
                    movingCoordinateTop = par.topMargin;

                    v.setLayoutParams(par);

                    break;
                }// inner case MOVE
                case MotionEvent.ACTION_UP: {

                    break;
                }// inner case UP
                case MotionEvent.ACTION_DOWN: {

                    v.setLayoutParams(par);
                    break;
                }// inner case UP
                }// inner switch
                break;
            }// case pawn2

            }// switch
            return true;
        }
    };

} 

また、 activity_main.xmlでビューを再配置する必要があります

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2"
    android:columnWidth="300dp"
    android:orientation="vertical"
    android:rowCount="2"
    android:stretchMode="columnWidth" >

    <FrameLayout
        android:id="@+id/topright"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="left"
        android:background="@drawable/shape" >

        <ImageView
            android:id="@+id/myimage1"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="150dp"
            android:src="@drawable/ic_launcher" >
        </ImageView>

        <ImageView
            android:id="@+id/myimage2"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_below="@+id/answer_option_1"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="200dp"
            android:src="@drawable/ic_launcher" >
        </ImageView>

        <ImageView
            android:id="@+id/myimage3"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="250dp"
            android:src="@drawable/ic_launcher" >
        </ImageView>
    </FrameLayout>

</GridLayout>
于 2013-10-29T06:47:29.667 に答える