我正在编写一款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
感谢您提前提供任何帮助。
答案 0 :(得分:1)
你需要设置你的&#34; Sky&#34;处理拖动事件。您可以使用一行执行此操作:
findViewById(R.id.topHalf).setOnDragListener(new ColonyHutDrag());
当拖动的对象在没有设置View
的{{1}}上发布时,没有任何东西可以处理OnDragListener
,因此您不需要处理ACTION_DROP
。 t有机会将拖动的View
重置为VISIBLE
。