Here i created a dragable imageview .This is my sample code(not given full code)
public class mainClass extends Activity
{
MyView obj1,obj2;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
AbsoluteLayout layout = new AbsoluteLayout(LearnKid.this);
layout.setLayoutParam开发者_JS百科s(new AbsoluteLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,0,0));
obj1 = new MyView(this);
obj2 = new MyView(this);
layout.addView(obj1);
layout.addView(obj2);
setContentView(layout);
}
}
class MyView extends ImageView
{
public MyView(Context context)
{
super(context);
}
public boolean dispatchTouchEvent(MotionEvent event)
{
center_X= (int)event.getRawX();
center_Y = (int)event.getRawY();
this.setLayoutParams(new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.WRAP_CONTENT,
AbsoluteLayout.LayoutParams.WRAP_CONTENT,center_X-10,center_Y-(70)));
}
}
Here i get 2 separate imageviews and also it is dragable. But how do i check whether the first image has crossed the second image. Because at that point i want to stop the dragable option.
Plz help
thanx in advance....
I'm going to provide the initial logic for this. You have to get the position of the item you are checking against and compare whether your dragged items x or y coordinate (or both depends on what you want to achieve) have passed the your second items x and/or y coordinate. You need a basic understanding of the Cartesian coordinate system.
This you can implement in the dragging logic or when you release the object (take the finger from it).
Now just to clear something else up. Is there any special reason you're using an AbsoluteLayout? If not then please don't use it as it is deprecated and it's usage isn't encouraged.
AABB axis aligned bounding boxes
each time you want to update the position and the box being dragged you test if they overlap
you can try and adapt this
axmin = gofd.getPosition().x-gofd.getObjectSize().x/2f;
axmax = gofd.getPosition().x+gofd.getObjectSize().x/2f;
aymin = gofd.getPosition().y-gofd.getObjectSize().y/2f;
aymax = gofd.getPosition().y+gofd.getObjectSize().y/2f;
Enumeration<Integer> e2 = myHash.getKeys();
while( e2. hasMoreElements() ){
GameObject toDraw = myHash.getGameObjectForID(e2.nextElement());
if(toDraw != null){
bymax = toDraw.getPosition().y+toDraw.getObjectSize().y/2f;
bymin = toDraw.getPosition().y-toDraw.getObjectSize().y/2f;
bxmin = toDraw.getPosition().x-toDraw.getObjectSize().x/2f;
bxmax = toDraw.getPosition().x+toDraw.getObjectSize().x/2f;
if(!(axmax<bxmin || bxmax <axmin || aymax<bymin || bymax <aymin)){
return true;
}
}
}
}
gofd = game object finger drag
this is a code snippet from an old version of my game
the myHash contains a reference to the images datastrucutre I was using, but it shouldn't far off what you need
精彩评论