me han hecho esta tarea mediante el uso de los siguientes conceptos:
Supongamos que tengo la imagen de Visión y de pulsación larga en él, imagen dentro de esta imagen Vista sería arrastrar poder y se coloca dentro de la otra vista (por ejemplo, Diseño relativo) Establezca MyClickListner en el método setOnLongClickListener() de la Vista de imagen.
private final class MyClickListener implements View.OnLongClickListener {
// called when the item is long-clicked
@Override
public boolean onLongClick(View view) {
// TODO Auto-generated method stub
// create it from the object's tag
ClipData.Item item = new ClipData.Item((CharSequence)view.getTag());
String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN };
ClipData data = new ClipData(view.getTag().toString(), mimeTypes, item);
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, //data to be dragged
shadowBuilder, //drag shadow
view, //local data about the drag and drop operation
0 //no needed flags
);
// view.setVisibility(View.INVISIBLE);
return true;
}
}
asentando después MyDragListner en disposición relativa (por ejemplo bigImageRelativeLayoutVw.setOnDragListener (nueva MyDragListener());)
class MyDragListener implements View.OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
int X=(int)event.getX();
int Y=(int)event.getY();
int touchX = 0,touchY=0;
// Handles each of the expected events
switch (event.getAction()) {
//signal for the start of a 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:
break;
//the user has moved the drag shadow outside the bounding box of the View
case DragEvent.ACTION_DRAG_EXITED:
// v.setBackground(normalShape); //change the shape of the view back to normal
break;
//drag shadow has been released,the drag point is within the bounding box of the View
case DragEvent.ACTION_DROP:
// if the view is the bottomlinear, we accept the drag item
if(v == bigImageRelativeLayoutVw) {
View view = (View) event.getLocalState();
touchX=X-viewCoords[0]-20;
touchY=Y-viewCoords[1]-20;
View view1=new View(getActivity());
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(30,30);
layoutParams.leftMargin =touchX;
layoutParams.topMargin = touchY;
view1.setBackgroundResource(R.drawable.heavy_damage);
view1.setLayoutParams(layoutParams);
RelativeLayout containView = (RelativeLayout) v;
containView.addView(view1);
view.setVisibility(View.VISIBLE);
} else {
View view = (View) event.getLocalState();
view.setVisibility(View.VISIBLE);
break;
}
break;
//the drag and drop operation has concluded.
case DragEvent.ACTION_DRAG_ENDED:
// v.setBackground(normalShape); //go back to normal shape
default:
break;
}
return true;
}
}
Qué pasa con el evento '' onFling' de GestureDetector' - es invocado después de levantar el dedo arriba de presionar y mover? Alternativamente, también puede probar "raw" 'onTouchEvent' ... – Xion
¿Está consumiendo LongPress? puede elegir aceptar el evento LongPress, pero luego devuelve falso, lo que permite que todo lo anterior controle el evento. También existe la opción de deshabilitar LongPress en la Vista cuando la atrape. –
@ Dr.J desafortunadamente LongPress no se puede consumir. El método devuelve void, no boolean – Lukas