我在我的应用中将拖动侦听器设置为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;
}
答案 0 :(得分:2)
您需要扩展 View.DragShadowBuilder ,并将手指的位置相对于控制器发送。这样的事情:
//where you trigger the dragging
View yourImage; //
yourImage.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final ClipData dragData = new ClipData("label", new String[]{ClipDescription.MIMETYPE_TEXT_PLAIN}, null);
startDrag(dragData, // the data to be dragged
new MyDragShadowBuilder(v, event.getX(), event.getY()), // the drag shadow builder
null,
0 // flags (not currently used, set to 0)
);
return true;
}
});
上课:
private static class MyDragShadowBuilder extends View.DragShadowBuilder {
int width, height, relativeX, relativeY;
public MyDragShadowBuilder(View v, float relativeX,float relativeY) {
super(v);
this.relativeX = (int) relativeX;
this.relativeY = (int) relativeY;
width = (v.getWidth());
height = (v.getHeight());
}
@Override
public void onProvideShadowMetrics (@NonNull Point size, @NonNull Point touch){
size.set(width, height);
touch.set(relativeX, relativeY);
}
}