我正在测试我的拖放安卓游戏。我让拖动的东西工作,但我希望将拖动的对象(imageview)放到另一个imageview上(在我的代码中我使用objBasket)
这是我到目前为止尝试过的代码:
mainLayout = (AbsoluteLayout) findViewById(R.id.mainLayout);
mainLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.i("touch", "touched here" + event);
if(status == 1) // any event from down and move
{
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,
(int)event.getX()-iv.get(0).getWidth()/2,
(int)event.getY()-iv.get(0).getHeight()/2);
iv.get(0).setLayoutParams(lp);
} else if(status == 2){
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,
(int)event.getX()-iv.get(1).getWidth()/2,
(int)event.getY()-iv.get(1).getHeight()/2);
iv.get(1).setLayoutParams(lp);
} else if(status == 3){
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,
(int)event.getX()-iv.get(2).getWidth()/2,
(int)event.getY()-iv.get(2).getHeight()/2);
iv.get(2).setLayoutParams(lp);
} else if(status == 4){
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,
(int)event.getX()-iv.get(3).getWidth()/2,
(int)event.getY()-iv.get(3).getHeight()/2);
iv.get(3).setLayoutParams(lp);
}
if(event.getAction() == MotionEvent.ACTION_UP){
status = 0;
iv.get(0).setBackgroundColor(Color.TRANSPARENT);
iv.get(1).setBackgroundColor(Color.TRANSPARENT);
iv.get(2).setBackgroundColor(Color.TRANSPARENT);
iv.get(3).setBackgroundColor(Color.TRANSPARENT);
} else if (event.getAction() == MotionEvent.ACTION_DOWN){
status = 0;
}
return true;
}
});
我的问题是我应该在MotionEvent.ACTION_DOWN中使用什么方法?我有点拖拉,所以我请你原谅。这里有人做过同样的事吗?请给我建议或示例代码。提前谢谢!
答案 0 :(得分:0)
这是我检查两个视图Rect bounds是否相交的方法
private boolean checkHit(View v, View hit){
Rect rv = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
Rect rhit = new Rect(hit.getLeft(), hit.getTop(), hit.getRight(), hit.getBottom());
return rv.intersect(rhit);
}
这是否属于
private boolean checkHit(MotionEvent event, View hit){
Rect rhit = new Rect(hit.getLeft(), hit.getTop(), hit.getRight(), hit.getBottom());
return rhit.contains((int)event.getRawX(), (int)event.getRawY());
}
然后只是浏览你感兴趣的观点......
编辑: 在你的ACTION_UP中调用这样的东西,我在切换的情况下用if语句
来做到这一点case MotionEvent.ACTION_UP:
drop(v, event);
break;
和drop方法
private void drop(View v, MotionEvent event){
checkHit(v, view2beHit);
checkHit(event, view2beHit);
// Do some for looping, do some other magic, do what you want
}
希望这有助于并享受您的工作。