我想将图像从一个地方移动到另一个地方。
现在图像由ImageView
保存,我正在使用OnDragListener
绘制拖放但它返回零点异常并且应用程序崩溃是否有任何其他方法来实现拖动并删除和解决此问题的任何解决方案。
这是我的主要活动代码:
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
ima = (ImageView)findViewById(R.id.iv_logo);
ima.setTag(IMAGEVIEW_TAG);
ima.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item((CharSequence)v.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData(v.getTag().toString(),
mimeTypes, item);
// Instantiates the drag shadow builder.
View.DragShadowBuilder myShadow = new DragShadowBuilder(ima);
// Starts the drag
v.startDrag(dragData, // the data to be dragged
myShadow, // the drag shadow builder
null, // no need to use local data
0 // flags (not currently used, set to 0)
);
return true;
}
});
}
这是我的拖放代码:
ima.setOnDragListener( new OnDragListener(){
@Override
public boolean onDrag(View v, DragEvent event){
switch(event.getAction())
{
case DragEvent.ACTION_DRAG_STARTED:
layoutParams = (RelativeLayout.LayoutParams)
v.getLayoutParams();
Log.d(msg, "Action is DragEvent.ACTION_DRAG_STARTED");
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENTERED");
int x_cord = (int) event.getX();
int y_cord = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_EXITED :
Log.d(msg, "Action is DragEvent.ACTION_DRAG_EXITED");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
layoutParams.leftMargin = x_cord;
layoutParams.topMargin = y_cord;
v.setLayoutParams(layoutParams);
break;
case DragEvent.ACTION_DRAG_LOCATION :
Log.d(msg, "Action is DragEvent.ACTION_DRAG_LOCATION");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_ENDED :
break;
case DragEvent.ACTION_DROP:
break;
default: break;
}
return true;
}
});
答案 0 :(得分:1)
我在活动中忘记了这一点:
super.onCreate(savedInstanceState);