1 つの場所に 4 つImageView
の s があり、RelativeLayout
それらがすべて重なり合うように配置されています。これらはすべてImageView
ドラッグ リスナーに登録され、「ドロップ ターゲット」になります。それらが表示する画像はすべて部分的に透明であるため、それらをすべて重ねると 1 つの画像のように見えます。
画面の他の場所には、上の円の 4 つのそれぞれに「適合」する 4 つのカラー画像があります。ここで、色付きの画像を円の上にドラッグ アンド ドロップするとImageView
、XML コードにリストされている 4 つの画像のうち、最後の画像だけがドロップされます。おそらくこれは「上」にあるため、ドロップイベント通知を受け取るのはこれだけです。
どういうわけか、ドロップが発生した場所を見つけ、ImageView
その場所に存在するすべての を選択して、正しい「ドロップ ターゲット」が存在するかどうかを確認し、それに応じてそれを更新する必要があります。これを達成する方法について、次の 2 つのアイデアがあります。
ImageView
ドロップが発生した X、Y 位置ですべての s の配列を取得する必要があります。次に、透明でないピクセルを持つものを見つけます。これは、ドラッグしているピースの正しいターゲットです。- ドロップが発生したときに、現在の「ドロップ ターゲット」
ImageView
がピースに一致するものではない場合、現在の他ImageView
のすべての をチェックしRelativeLayout
て、そこに正しいものがあるかどうかを確認する必要があります。
残念ながら、これらのいずれかを達成する方法がわかりません。私の質問は基本的に、スタックImageView
s を持っているときに簡単なオプションがありませんか? そうでない場合は、上記の 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 がそのポイントを持っているかどうかを確認する方法がわかりません。