【发布时间】:2015-08-03 08:26:40
【问题描述】:
我在我的应用程序中为ImageView 设置了一个拖动侦听器,但是当我单击它时,我不希望它根据我按下的位置将图像居中。它这样做:
https://gfycat.com/ConstantDisguisedKudu
基本上,如果我按下图像的bottom right,它会将我按下的位置设为central point,并将图像的center point 移动到该确切位置。但我不希望它那样做。如果我按下 bottom right ,它不应该自动移动,我可以从那个点拖动图像。我认为不需要任何代码,但以防万一:
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
// Signal for the start of drag and drop operation
case DragEvent.ACTION_DRAG_STARTED: {
// do nothing
break;
}
// The drag point has entered the bounding box of the View
case DragEvent.ACTION_DRAG_ENTERED: {
// do nothing
break;
}
// The user has moved the drag shadow outside the bounding box of the view
case DragEvent.ACTION_DRAG_EXITED: {
// do nothing
break;
}
// Drag shadow has been released, the drag point is within the bounding box of the view
case DragEvent.ACTION_DROP: {
// Get the image and its position
ImageView view = (ImageView) event.getLocalState();
int position = (int) view.getTag(R.id.piece_position);
/**
* If it is dropped on the left pane, remove it from its parent and also
* remove the bitmap at the position and notify the adapter.
* Add it to the left pane and set the position.
*/
if (v == puzzlePane) {
ViewGroup viewgroup = (ViewGroup) view.getParent();
viewgroup.removeView(view);
if (position != -1) {
pieces.remove(position);
mAdapter.notifyDataSetChanged();
}
FrameLayout containView = (FrameLayout) v;
containView.addView(view);
view.setVisibility(View.VISIBLE);
view.setTag(R.id.piece_state, "left");
view.setTag(R.id.piece_position, -1);
view.setOnLongClickListener(null);
view.setOnTouchListener(mAdapter);
} else {
view.setVisibility(View.VISIBLE);
view.setTag(R.id.piece_state, "right");
view.setOnTouchListener(null);
view.setOnLongClickListener(mAdapter);
}
Log.d(MyDragListener.class.getSimpleName(), view.getTag(R.id.piece_state) + "");
view.setX(event.getX() - (view.getWidth() / 2));
view.setY(event.getY() - (view.getHeight() / 2));
break;
}
// The drag and drop operation has concluded
case DragEvent.ACTION_DRAG_ENDED: {
// do nothing
break;
}
}
return true;
}
【问题讨论】:
标签: android drag-and-drop onclicklistener drag ontouchlistener