gpt4 book ai didi

java - 拖放图像按钮消失在不同 View 后面

转载 作者:行者123 更新时间:2023-12-01 12:43:08 27 4
gpt4 key购买 nike

我正在编写一款Android游戏,用户可以购买在购买后动态创建的建筑物。创建它们后,用户可以将它们拖放到任何想要的地方,只要它们在地面上即可。我将天空和地面作为两种不同的框架布局,但天空占据顶部的 25%,地面占据底部的 75%。当用户拖动建筑物时,它们会按照应有的方式行事,直到用户尝试将其拖动到天空区域。它不会像我希望的那样快速回到原来的位置,而是消失在天空区域后面。

这是顶部和底部的 xml:

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/mars"
android:id="@+id/gameBackground" >

<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.75"
android:orientation="horizontal"
android:id="@+id/topHalf" >

</FrameLayout>

<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:orientation="horizontal"
android:id="@+id/bottomHalf" >

<ImageButton
android:id="@+id/polarCapButton1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|left"
android:background="@drawable/polarcap" />

<ImageButton
android:id="@+id/polarCapButton2"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:background="@drawable/polarcap" />

</FrameLayout>

</LinearLayout>

以下是创建图像按钮的代码:

frame = (FrameLayout) findViewById(R.id.bottomHalf);
newColonyHut = new ImageButton(runGraphics.this);
newColonyHut.setBackgroundResource(R.drawable.mainhut);
newColonyHut.setTag("NewColonyHut");
newColonyHut.setOnTouchListener(new ColonyHutClick());
findViewById(R.id.bottomHalf).setOnDragListener(new ColonyHutDrag());
FrameLayout.LayoutParams param = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
frame.addView(newColonyHut, param);

这是 ColonyHutClick 类:

public class ColonyHutClick implements OnTouchListener 
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
ClipData.Item item = new ClipData.Item((CharSequence)v.getTag());

String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN };
ClipData data = new ClipData(v.getTag().toString(), mimeTypes, item);
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);

v.startDrag(data, shadowBuilder, v, 0);
v.setVisibility(View.INVISIBLE);

return false;
}//end onTouch function
}//end ColonyHutClick class

这是 ColonyHutDrag 类:

public class ColonyHutDrag extends Activity implements OnDragListener
{

@Override
public boolean onDrag(View v, DragEvent event)
{
int x = 0, y = 0;
switch(event.getAction())
{
case DragEvent.ACTION_DRAG_STARTED:
//drag has started
break;

case DragEvent.ACTION_DRAG_ENTERED:
//being dragged
break;

case DragEvent.ACTION_DRAG_EXITED:
//stop drag
break;

case DragEvent.ACTION_DRAG_LOCATION:
//find drag location
break;

case DragEvent.ACTION_DROP:
if (v == v.findViewById(R.id.bottomHalf))
{
//find position where dropped
x = (int) event.getX();
y = (int) event.getY();


View view = (View) event.getLocalState();
ViewGroup group = (ViewGroup) view.getParent();
group.removeView(view);
FrameLayout contain = (FrameLayout) v;
contain.addView(view);
view.setX(x - (view.getWidth()/2));
view.setY(y - (view.getHeight()/2));
view.setVisibility(View.VISIBLE);
}//end if
else
{
View view = (View) event.getLocalState();
view.setVisibility(View.VISIBLE);
}//end else
break;

default:
break;
}//end switch
return true;
}//end onDrag function
}//end ColonyHutDrag class

感谢您提前提供的任何帮助。

最佳答案

您需要设置“Sky”来处理拖动事件。您只需一行即可完成此操作:

 findViewById(R.id.topHalf).setOnDragListener(new ColonyHutDrag());

当通过没有设置 OnDragListenerView 释放拖动的对象时,没有任何东西可以处理 ACTION_DROP,因此,您没有机会将拖动的 View 重置为 VISIBLE

关于java - 拖放图像按钮消失在不同 View 后面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24900769/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com