I have to admit that I swore here, this is a hack, but it works fine. I started with the need to know when the scale occurred, and as soon as I connected to it (and after some interesting debugging), I found that some of the values โโwere โbetween the scaling valuesโ, so I had to wait until the scale was completed.
As suggested elsewhere in Stack Overflow, my zoom viewer is an overridden MapView.dispatchDraw that checks if the zoom level has changed the last time.
In addition to this, I added the isResizing method, which checks if the timestamp is more than 100 ms, since the getLongitudeSpan value has stopped changing. It works great. Here is the code:
My first post! Whoo Hoo!
Public class MapViewWithZoomListener extends MapView {
private int oldZoomLevel = -1; private List<OnClickListener> listeners = new ArrayList<OnClickListener>(); private long resizingLongitudeSpan = getLongitudeSpan(); private long resizingTime = new Date().getTime(); public MapViewWithZoomListener(Context context, String s) { super(context, s); } public MapViewWithZoomListener(Context context, AttributeSet attributeSet) { super(context, attributeSet); } public MapViewWithZoomListener(Context context, AttributeSet attributeSet, int i) { super(context, attributeSet, i); } public boolean isResizing() { // done resizing if 100ms has elapsed without a change in getLongitudeSpan return (new Date().getTime() - resizingTime < 100); } public void dispatchDraw(Canvas canvas) { super.dispatchDraw(canvas); if (getZoomLevel() != oldZoomLevel) { new AsyncTask() { @Override protected Object doInBackground(Object... objects) { try { if (getLongitudeSpan() != resizingLongitudeSpan) { resizingLongitudeSpan = getLongitudeSpan(); resizingTime = new Date().getTime(); } Thread.sleep(125); //slightly larger than isMoving threshold } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Object o) { super.onPostExecute(o); if (!isResizing() && oldZoomLevel != getZoomLevel()) { oldZoomLevel = getZoomLevel(); invalidate(); for (OnClickListener listener : listeners) { listener.onClick(null); } } } }.execute(); } } public void addZoomListener(OnClickListener listener) { listeners.add(listener); }
source share