我正在使用MapView
创建Android应用。我需要对用户的触摸事件做出反应。
我尝试使用onTouchEvent
处理dispatchTouchEvent
中的某些操作。但是这个onTouchEvent
没有被调用!当我在dispatchTouchEvent
中添加此代码时:
MarkerOverlay myMarker = new MarkerOverlay(marker,edTxtAlarmClockName.getText().toString());
mapView.getOverlays().add(myMarker);
mapView.invalidate();
调用 onTouchEvent
。为什么会如此以及如何描述overlay
处理程序?
public class MainActivity extends MapActivity implements OnTouchListener{
private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
private MyLocationOverlay myLocationOverlay;
private Context context;
public GeoPoint lastClickedPoint;
Dialog dialog;
EditText edTxtAlarmClockName;
ToggleButton tbOnOffAlarmClock;
boolean reClick ;
private int lastTouchX, lastTouchY;
private boolean hasMoved = false;
Button btnOk;
Button btnCancelDelete;
Button btnAlarmClockStop;
public int distance=50;
private int indexOfSelectedOverlay;
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_main);
context = MainActivity.this;
// Configure the Map
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setStreetView(true);
mapView.setSatellite(true);
mapView.setOnTouchListener(this);
mapController = mapView.getController();
mapController.setZoom(15);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 10, geoupdate);
myLocationOverlay = new MyLocationOverlay(this, mapView);
myLocationOverlay.disableCompass();
myLocationOverlay.enableMyLocation();
mapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
mapView.getController().animateTo(myLocationOverlay.getMyLocation());
mapView.setBuiltInZoomControls(true);
}
});
final Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
dialog = new Dialog(context);
dialog.setContentView(R.layout.custom1);
dialog.setTitle(R.string.str_new_alarm);
tbOnOffAlarmClock=(ToggleButton)dialog.findViewById(R.id.toggleButton1);
edTxtAlarmClockName = (EditText) dialog.findViewById(R.id.textView1);
edTxtAlarmClockName.setText(R.string.str_name);
btnOk = (Button) dialog.findViewById(R.id.btn_ok);
btnCancelDelete = (Button) dialog.findViewById(R.id.btn_cancel);
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MarkerOverlay myMarker;
if (reClick) //click on the overlay
{//search and rename
myMarker=(MarkerOverlay)mapView.getOverlays().get(indexOfSelectedOverlay);
mapView.getOverlays().remove(indexOfSelectedOverlay);
myMarker.setName(edTxtAlarmClockName.getText().toString());
mapView.getOverlays().add(myMarker);
mapView.invalidate();
}
else
{//creat overlay
myMarker = new MarkerOverlay(drawable,edTxtAlarmClockName.getText().toString());
myMarker.setName(edTxtAlarmClockName.getText().toString());
if (tbOnOffAlarmClock.isChecked()) {
myMarker.active=true;
} else {
myMarker.active=false;
}
mapView.getOverlays().add(myMarker);
mapView.invalidate();
}
dialog.dismiss();
}
});
btnCancelDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
OverlayItem item;
Point itemPoint,currentPoint;
MarkerOverlay myOverlay;
if (reClick) //delete marker
{
Log.d("debug","delete");
mapView.getOverlays().remove(indexOfSelectedOverlay);
mapView.invalidate();
}
//else CANCEL
dialog.dismiss();
}
});
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
protected void onDestroy() {
super.onDestroy();
locationManager.removeUpdates(geoupdate);
}
@Override
protected void onResume() {
super.onResume();
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
}
@Override
protected void onPause() {
super.onPause();
myLocationOverlay.disableMyLocation();
myLocationOverlay.disableCompass();
}
public class MarkerOverlay extends ItemizedOverlay<OverlayItem>
{
private Drawable marker = null;
private boolean active=true;
private GeoPoint pointOfOverlay;
private String customName;
public List<OverlayItem> items=new ArrayList<OverlayItem>();;
public MarkerOverlay(Drawable marker,String name)
{
super(boundCenterBottom(marker));
this.marker=marker;
OverlayItem item = new OverlayItem(lastClickedPoint, name,"");
this.pointOfOverlay=lastClickedPoint;
this.customName=name;
items.add(item);
populate();
}
public String getName()
{
return customName;
}
public void setName(String name)
{
customName=name;
}
public GeoPoint getPoint()
{
return pointOfOverlay;
}
@Override
protected OverlayItem createItem(int i)
{
return(items.get(i));
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{
super.draw(canvas, mapView, shadow);
boundCenterBottom(marker);
}
@Override
public int size()
{
return(items.size());
}
@Override
public boolean onTouchEvent(MotionEvent motionEvent, MapView mapView) {
boolean consumed = super.onTouchEvent(motionEvent,mapView);
int action = motionEvent.getAction();
int size;
switch (action) {
case MotionEvent.ACTION_DOWN:
lastTouchX =(int) motionEvent.getX();
lastTouchY =(int) motionEvent.getY();
hasMoved = false;
break;
case MotionEvent.ACTION_MOVE:
hasMoved = moved(motionEvent);
break;
case MotionEvent.ACTION_UP:
if (!moved(motionEvent))
{
size = items.size();
if (size > 0)
{
OverlayItem item = items.get(size - 1);
Point p1 = mapView.getProjection().toPixels(item.getPoint(), null);
if (hitTest(item, marker, lastTouchX - p1.x, lastTouchY - p1.y))
{
dialog.setTitle(item.getTitle());
btnCancelDelete.setText(R.string.str_delete);
dialog.show();
reClick=true;
}
else
{
edTxtAlarmClockName.setText(R.string.str_name);
btnCancelDelete.setText(R.string.str_cancel);
dialog.show();
lastClickedPoint = mapView.getProjection().fromPixels((int) motionEvent.getX(), (int) motionEvent.getY());
reClick=false;
}
populate();
return true;
}
else
{
edTxtAlarmClockName.setText(R.string.str_name);
dialog.setTitle(R.string.str_new_alarm);
btnCancelDelete.setText(R.string.str_cancel);
dialog.show();
lastClickedPoint = mapView.getProjection().fromPixels((int) motionEvent.getX(), (int) motionEvent.getY());
reClick=false;
}
populate();
return true;
}
}
return false||consumed;
}
private boolean moved(MotionEvent evt) {
Log.d("debug", "ontouchevent move");
return hasMoved ||
Math.abs(evt.getX() - lastTouchX) > 20.0 ||
Math.abs(evt.getY() - lastTouchY) > 20.0;
}
}
@Override
public boolean onTouch (View v, MotionEvent event)
{
int action = event.getAction();
if (action == MotionEvent.ACTION_UP)
{
lastClickedPoint= mapView.getProjection().fromPixels( (int) event.getX(), (int) event.getY());
return true;
}
return false;
}
@Override
public boolean dispatchTouchEvent(MotionEvent motionEvent) {
int action = motionEvent.getAction();
Drawable marker = getResources().getDrawable(R.drawable.ic_launcher);
switch(action) {
case MotionEvent.ACTION_UP:
MarkerOverlay myMarker = new MarkerOverlay(marker,edTxtAlarmClockName.getText().toString());
mapView.getOverlays().add(myMarker);
mapView.invalidate();
break;
}
return super.dispatchTouchEvent(motionEvent);
}
}
答案 0 :(得分:0)
我认为您应该将onTouchEvents的过程添加到触摸中,同时将MapView作为全局变量,然后您可以在覆盖的onTuch事件中调用Mapview变量。
据我所知,启动onTouchEvents
没有任何联系