我正在测试我的拖放安卓游戏。我让拖动的东西正常工作,但我希望将拖动的对象(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 中使用什么方法?我对拖放东西有点陌生,所以请原谅。这里有人做过同样的事情吗?请给我建议或示例代码。提前致谢!