0

下にある画像をドラッグアンドドロップして移動するコードがあります

その中で、ボタンを押すことで動的に画像を追加しています。

しかし、画像を追加すると、前の画像のタッチが失われました。つまり、画像を追加して前の画像を移動することはできません。新しく作成された画像しか制御できません

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;
private Button butClick;
int i = 0;
/** 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);


    emptyLetterView = (ImageView) findViewById(R.id.emptyLetterView);
    butClick = (Button)findViewById(R.id.button1);
    butClick.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            letterView =  new ImageView(DragAndDropBasicActivity.this);
            letterView.setId(i++);
            letterView.setImageResource(R.drawable.hk_a1);
            letterView.setOnTouchListener(DragAndDropBasicActivity.this);
            mainLayout.addView(letterView);
        }
    });

}

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);
}

}

以前に追加した画像を移動できるように、誰かが以前に追加した画像を制御するのを手伝ってくれませんか。

4

1 に答える 1

3

最後に私は答えを得ました私はimageviewの配列を作成し、次に各imageviewを比較する必要があります

ArrayList<ImageView> mArrayList = new ArrayList<ImageView>();

その後

       public void onClick(View v) {
            // TODO Auto-generated method stub
            letterView =  new ImageView(DragAndDropBasicActivity.this);
            mArrayList.add(letterView);
            letterView.setId(i++);
            letterView.setImageResource(R.drawable.hk_a1);
            letterView.setOnTouchListener(DragAndDropBasicActivity.this);
            mainLayout.addView(letterView);
        }

今私はチェックする必要があります

    public boolean onTouch(View v, MotionEvent event) {
    boolean eventConsumed = true;
    int x = (int)event.getX();
    int y = (int)event.getY();
    for (ImageView img : mArrayList) {
        if(v == img){
            letterView = img;
        }
    }

このようにして私は解決策を手に入れましたYuppy!!!!!!!!

于 2012-08-30T09:07:12.240 に答える