Ho trovato un po 'di ottimizzazione per la funzione brillante del drag and drop di CommonsWare.
Stavo facendo alcune misurazioni precise con i marcatori sulla mia mappa, e volevo che il mio marcatore fosse esattamente sul punto che ho toccato e sollevato il dito in modo che il misuramento fosse esattamente preciso.
Sull'originale da CommonsWare se si tocca "vicino" al marker, il marker non va a quel punto esatto, ma si muove insieme al movimento del dito. Avevo bisogno che il marcatore appaia appena sotto il mio dito su ACTION_DOWN, ACTION_MOVE e ACTION_UP.
Ecco il codice se qualcuno ne ha bisogno. Devo ringraziare CommonsWare per realizzare questa funzione, è davvero una buona idea.
Questa è la parte del codice che ho modificato.
My MapView è mapa;
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
final int action=event.getAction();
final int x=(int)event.getX();
final int y=(int)event.getY();
boolean result=false;
if (action==MotionEvent.ACTION_DOWN) {
for (OverlayItem item : mOverlays) {
Point p=new Point(0,0);
mapa.getProjection().toPixels(item.getPoint(), p);
//I maintain the hitTest's bounds so you can still
//press near the marker
if (hitTest(item, marker, x-p.x, y-p.y)) {
result=true;
inDrag=item;
mOverlays.remove(inDrag);
populate();
//Instead of using the DragImageOffSet and DragTouchOffSet
//I use the x and y coordenates from the Point
setDragImagePosition(x, y);
dragImage.setVisibility(View.VISIBLE);
break;
}
}
}
else if (action==MotionEvent.ACTION_MOVE && inDrag!=null) {
setDragImagePosition(x, y);
result=true;
}
else if (action==MotionEvent.ACTION_UP && inDrag!=null) {
dragImage.setVisibility(View.GONE);
//I get the geopoint without using any OffSet, directly with the
//Point coordenates
GeoPoint pt=mapa.getProjection().fromPixels(x,y);
OverlayItem toDrop=new OverlayItem(pt, inDrag.getTitle(),
inDrag.getSnippet());
orig = inDrag.getMarker(0);
//In my case I had down heading Arrows as markers, so I wanted my
//bounds to be at the center bottom
toDrop.setMarker(boundCenterBottom(orig));
mOverlays.add(toDrop);
populate();
inDrag=null;
result=true;
}
return(result || super.onTouchEvent(event, mapView));
}
private void setDragImagePosition(int x, int y) {
RelativeLayout.LayoutParams lp=
(RelativeLayout.LayoutParams)dragImage.getLayoutParams();
//Instead of using OffSet I use the Point coordenates.
//I want my arrow to appear pointing to the point I am moving, so
//I set the margins as the Point coordenates minus the Height and half the
//width of my arrow.
lp.setMargins(x-(dragImage.getWidth()/2),y-dragImage.getHeight(), 0, 0);
dragImage.setLayoutParams(lp);
}
Con questo si ottiene la freccia che appare in cui si preme con il vostro, al posto della freccia in movimento dal punto in cui è stato.
Grazie mille per il collegamento e la spiegazione! Sono nuovo di Android, quindi è bene per me guardare la struttura di un progetto ben funzionante, per avere l'idea di come fare le cose per davvero e non solo gettare le cose lì dentro. Grazie. – kakka47
Thnx man, questo mi ha davvero aiutato – binW
Penso che questa volta sia obsoleto. bcoz v2 dosenot overlay di supporto. Hai qualcosa per V2 anche –