I am programming a game which the user can purchase buildings that, when purchased, are created dynamically and then appear to the user. They are supposed to be drag and drop so that they can be moved where the user wants them. I two framelayouts that take up the screen, a bottom half and a top half. The user should be able to move the buildings where they want as long as it is on the bottom half. The top half should automatically make the building pop back in place. The issue I have is that no matter where the user drags the buildings, they automatically snapped back to where they were originally.
Here is the code for the setup of the imagebutton and drag and drop:
FrameLayout 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.gameLayout).setOnDragListener(new ColonyHutDrag());
//newColonyHut.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
FrameLayout.LayoutParams param = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
frame.addView(newColonyHut, param);
Here is the ColonyHutClick class:
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
Here is the ColonyHutDrag class:
public class ColonyHutDrag 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
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);
//LinearLayout contain = (LinearLayout) v;
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);
//Context context = getApplicationContext();
}//end else
break;
default:
break;
}//end switch
return true;
}//end onDrag function
}//end ColonyHutDrag class
Any help is appreciated. Thanks guys.
No comments:
Post a Comment