3

私はこの「ドラッグアンドドロップ」のものを持っています。

基本的には、ユーザー (子供) がキャンディーを瓶にドラッグ アンド ドロップできるようにするためのプロトタイプを作成しています。

これらは私がしばらく研究してきたコードです

package edu.sbcc.cs123.draganddropbasic;

import android.app.*;
import android.graphics.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;

@SuppressWarnings("deprecation")
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) {
                dragging = true;
                eventConsumed = false;
            }
        } else if (action == MotionEvent.ACTION_UP) {

            if (dragging) {
                emptyLetterView.getHitRect(hitRect);
                if (hitRect.contains(x, y))
                    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);
    }
}

これらは main.xml 用です

<?xml version="1.0" encoding="utf-8"?>


<AbsoluteLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainLayout">

<ImageView 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:id="@+id/emptyLetterView" 
    android:src="@drawable/candy" 
    android:layout_x="200px" 
    android:layout_y="300px"></ImageView>

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

</ImageView>
</AbsoluteLayout>

ただし、ドラッグする他のキャンディーも追加したいと思います。どうすればこれを実装できますか?

4

1 に答える 1

2

わかりました、あなたの初心者は私がこれを徹底的に調べます。

あなたが選択した方法は、あなたが試みているものには理想的ではありません.カスタムビューとキャンバスを使用する方が良いかもしれません.

ただし、この方法で必要なことを行うことはできます (また、書き直しを回避できます)。

ここでの問題は、ImageView複数のインスタンスが必要な場合に 1 つあることです。

したがって、複数のインスタンスを動的に作成する必要があるため、最初に XML ファイルから candy を削除してから、次のコードに従います

ImageView[] candies = new ImageView[10];
 for(int i=0;i<candies.length;i++){
      candies[i]=new ImageView(this);
      // TODO: Set LayoutParams for each imageView
      // i.e. AbsoluteLayout.LayoutParams imageParams1 = new AbsoluteLayout.LayoutParams(imageWidth, imageHeight); 

     candies[i].setBackgroundResource(R.drawable.candy); 
     layout.addView(candies[i], imageParams1);
}

画面に n 個のキャンディーが追加されたので、それらを移動できるようにする必要があります。

のコードをonTouch編集する必要があります。

boolean draggingドラッグする代わりに、intどのビューがドラッグされているかを示します。

また、代わりに

setSameAbsoluteLocation(letterView, emptyLetterView);

使用する

setSameAbsoluteLocation(v, emptyLetterView);

そのため、コードはリッスンしているすべてのビュー (つまり、すべてのキャンディー) で機能します。と のすべてのletterView場所でこれをMOUSE_UP行いますMOUSE_DOWN

MOUSE_MOVE適切なビューをドラッグするには、次のようなことを行う必要があります

if (v != letterView) {
    if (dragging!=-1) {
        setAbsoluteLocationCentered(letterView[dragging], x, y);
     }
}
于 2013-01-11T15:58:27.683 に答える