我正在android上制作一个小型应用程序,它基于带有shadowbuilder的拖放功能,我使用了带有屏幕wvga800(800x480)的模拟器,它工作正常,当将元素拖动到网格时,它会正确地落在目标网格中。然后我使用了另一个屏幕 wxga800-7in (1280-800) 的模拟器,一切都出错了,元素不再落在网格内,它们似乎移动到另一个位置。注意:在布局 XML 文件中使用的单位是 dp。这里到底有什么问题?请帮忙。
提前感谢您的帮助。
以下是触摸监听器和拖动监听器的代码:
private final class MyTouchListener implements OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view,0);
view.setVisibility(View.INVISIBLE);
return true;}
else {
return false;
}
}
}//class MyTouchListener
class MyDragListener implements View.OnDragListener {
Drawable enterShapeAcp = getResources().getDrawable(R.drawable.shape_droptargetacp);
Drawable enterShapeRej= getResources().getDrawable(R.drawable.shape_droptargetrej);
Drawable normalShape = getResources().getDrawable(R.drawable.shape);
@Override
public boolean onDrag(View v, DragEvent event) {
// int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:{
break;}
case DragEvent.ACTION_DRAG_ENTERED:{
grid(v.getX(),v.getY());
v.setBackgroundDrawable(enterShapeAcp);
break;}
case DragEvent.ACTION_DRAG_EXITED:{
v.setBackgroundDrawable(normalShape);
break;}
case DragEvent.ACTION_DROP:{
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) v;
container.addView(view);
view.setVisibility(View.VISIBLE);
break;}
case DragEvent.ACTION_DRAG_ENDED:{
v.setBackgroundDrawable(normalShape);
break;}
default:{
break;}
}//switch
return true;
}//onDrag
}//class MyDragListener