0

1 つの場所に 4 つImageViewの s があり、RelativeLayoutそれらがすべて重なり合うように配置されています。これらはすべてImageViewドラッグ リスナーに登録され、「ドロップ ターゲット」になります。それらが表示する画像はすべて部分的に透明であるため、それらをすべて重ねると 1 つの画像のように見えます。

画面の他の場所には、上の円の 4 つのそれぞれに「適合」する 4 つのカラー画像があります。ここで、色付きの画像を円の上にドラッグ アンド ドロップするとImageView、XML コードにリストされている 4 つの画像のうち、最後の画像だけがドロップされます。おそらくこれは「上」にあるため、ドロップイベント通知を受け取るのはこれだけです。

どういうわけか、ドロップが発生した場所を見つけ、ImageViewその場所に存在するすべての を選択して、正しい「ドロップ ターゲット」が存在するかどうかを確認し、それに応じてそれを更新する必要があります。これを達成する方法について、次の 2 つのアイデアがあります。

  1. ImageViewドロップが発生した X、Y 位置ですべての s の配列を取得する必要があります。次に、透明でないピクセルを持つものを見つけます。これは、ドラッグしているピースの正しいターゲットです。
  2. ドロップが発生したときに、現在の「ドロップ ターゲット」ImageViewがピースに一致するものではない場合、現在の他ImageViewのすべての をチェックしRelativeLayoutて、そこに正しいものがあるかどうかを確認する必要があります。

残念ながら、これらのいずれかを達成する方法がわかりません。私の質問は基本的に、スタックImageViews を持っているときに簡単なオプションがありませんか? そうでない場合は、上記の 2 つのアイデアのいずれかをどのように達成できますか?


これはイメージの例です。円のこれら 4 つの「セクション」はそれぞれ、透明な背景を持つ個別のイメージです。


それらを一緒に配置するために使用した切り捨てられた XML コードは次のとおりです。

<RelativeLayout 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
       >
   <ImageView
    android:id="@+id/circlepuz1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="false"
    android:src="@drawable/circle_part1"
    android:layout_gravity="center_horizontal"
    android:tag="bottom_right" />

   <ImageView
    android:id="@+id/circlepuz2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@id/circlepuz1"
    android:layout_alignRight="@id/circlepuz1"
    android:layout_alignTop="@id/circlepuz1"
    android:layout_alignBottom="@id/circlepuz1"
    android:scaleType="fitXY"
    android:src="@drawable/circle_part2"
    android:tag="bottom_left" />

...
Every other ImageView is the same id+1, allignXXX=@id/circlepuz1

現在の Java ドロップ コードは次のとおりです。

case DragEvent.ACTION_DROP:        
    //handle the dragged view being dropped over a target view
    View view = (View) draggedView.getLocalState(); 
    //stop displaying the view where it was before it was dragged
    view.setVisibility(View.INVISIBLE); 

    //view dragged item is being dropped on
    ImageView dropTarget = (ImageView) dropTargetView; 

    //view being dragged and dropped
    ImageView dropped = (ImageView) view;

    // I name the "drag" ImageView's tag and the "drop" ImageView's tag the same thing
    // so if they match, I know the piece is in the right place
    Object tag = dropped.getTag();
    Object dtag = dropTarget.getTag();

    if(tag.toString() != dtag.toString()){   // piece was in the wrong place
      Log.d(TAG, tag.toString() + " was not " + dtag.toString());
      view.setVisibility(View.VISIBLE); 
    }
    else {   // piece is in the right place
      dropTarget.setImageDrawable(dropped.getDrawable());
    }

最後にループしたいif/同じレイアウトのelseそれぞれをチェックしてImageView、正しいレイアウトが存在するかどうかを確認したいのですが、次のようなことを試しました:

    // Attempt to get the new ImageView 
    //ImageView test = (ImageView) findViewById(dropTarget.getNextFocusDownId());

これはまったく機能しませんでした。もう1つの考えは、簡単に実行できる「ドロップ」のXとYを取得することでした:

    String toastText = "X: " + draggedView.getX() + "\nY:" + draggedView.getY();
    Toast.makeText(getApplicationContext(), toastText, Toast.LENGTH_LONG).show();

ImageViewしかし、ここから、他の s がそのポイントを持っているかどうかを確認する方法がわかりません。

4

1 に答える 1

0

私は2番目のオプションに行きました:

「2.ドロップが発生したときに、現在の「ドロップ ターゲット」ImageView がピースに一致するものではない場合、現在の RelativeLayout 内の他のすべての ImageView をチェックして、そこに正しいものがあるかどうかを確認する必要があります。」

答えは思ったよりも簡単でしImageViewた。同じ場所にある他のすべての s を取得するには、親に確認して現在ImageViewの s (ViewGroup で利用可能) のリストを取得するだけです。一致するものを探して設定したタグを繰り返します。

case DragEvent.ACTION_DROP:        
    //handle the dragged view being dropped over a target view
    View view = (View) draggedView.getLocalState(); 

    //stop displaying the view where it was before it was dragged
    view.setVisibility(View.INVISIBLE); 

    //view dragged item is being dropped on
    ImageView dropTarget = (ImageView) dropTargetView; 
    //view being dragged and dropped
    ImageView dropped = (ImageView) view;

    // In this case the "dropTarget" is the "top most" ImageView of my stacked circles
    // I can create a ViewGroup of the parent of this ImageView (which is the 
    // RelativeLayout) in order to check all the children (other ImageViews)
    ViewGroup vg = (ViewGroup) dropTarget.getParent();

    Object tag = dropped.getTag();

    // Now I can iterate through each of the children (ImageViews) in the ViewGroup
    // looking to see if the dragged puzzle piece is a match for one of the "drop"
    // ImageViews
    for(int i = 0; i<vg.getChildCount(); i++){
        ImageView temp = (ImageView) vg.getChildAt(i);
        Object dtag = temp.getTag();
            if(dtag.toString().equals(tag.toString())){
                // Then we have a match! Place the piece
于 2013-10-07T19:29:29.370 に答える