我在一个ImageView
中放置了4 RelativeLayout
个,以便它们全部堆叠在一起。所有这些ImageView
已经注册了我的拖拽监听器,使它们成为“放弃目标”。它们显示的图像都是部分透明的,这样当它们全部放在一起时,它们看起来就是一个单一的图像。
屏幕上的其他地方有4个彩色图像,“适合”上面圆圈中的4个。现在,当我将彩色图像拖到圆圈上并放下它们时,唯一得到丢弃的ImageView
是XML代码中列出的四个中的最后一个。据推测,这个是“在顶部”,因此是唯一一个获得丢弃事件通知的人。
不知何故,我需要找到丢弃发生的位置并选择该位置存在的所有ImageView
,以便我可以看到是否存在正确的“放置目标”并相应地更新该放置目标。我有两个关于如何实现这个目标的想法:
ImageView
的数组,然后找到一个具有非透明像素的数组,这是我正在拖动的部分的正确目标。 ImageView
不是与该作品匹配的那个,我需要检查当前ImageView
中的所有其他RelativeLayout
看那里我是否有合适的人。不幸的是,我不知道如何完成其中任何一项。我的问题基本上是,我在堆叠ImageView
时错过了一个更简单的选项,如果我不是,那么我怎么能完成上述两个想法中的任何一个。
这是图像的一个示例,圆圈的这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());
根本不起作用。另一个想法是获得X&amp; Y的“掉落”很容易做到:
String toastText = "X: " + draggedView.getX() + "\nY:" + draggedView.getY();
Toast.makeText(getApplicationContext(), toastText, Toast.LENGTH_LONG).show();
但我不确定从这里开始检查其他ImageView
中有哪些内容。
答案 0 :(得分:0)
我最后选择了第二个选项:
“2。当发生丢弃时,如果当前的”放置目标“ImageView不是匹配该块的那个,我需要检查当前RelativeLayout中的所有其他ImageView以查看我是否有正确的ImageView。 “
答案最终变得比我想象的要容易,为了让所有其他ImageView
在同一个地方,我只需要与父母一起检查以获得礼物清单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