animate buildings
This commit is contained in:
@@ -14,15 +14,135 @@
|
||||
*/
|
||||
package org.oscim.overlay;
|
||||
|
||||
import org.oscim.core.MapPosition;
|
||||
import org.oscim.renderer.overlays.ExtrusionOverlay;
|
||||
import org.oscim.view.MapView;
|
||||
|
||||
import android.os.CountDownTimer;
|
||||
import android.util.Log;
|
||||
import android.view.MotionEvent;
|
||||
|
||||
/**
|
||||
* @author Hannes Janetzek
|
||||
*/
|
||||
public class BuildingOverlay extends Overlay {
|
||||
private final static String TAG = BuildingOverlay.class.getName();
|
||||
|
||||
final ExtrusionOverlay mExtLayer;
|
||||
|
||||
public BuildingOverlay(MapView mapView) {
|
||||
super();
|
||||
mLayer = new ExtrusionOverlay(mapView);
|
||||
mMapView = mapView;
|
||||
mExtLayer = new ExtrusionOverlay(mapView);
|
||||
mLayer = mExtLayer;
|
||||
}
|
||||
|
||||
private final MapView mMapView;
|
||||
private int multi;
|
||||
|
||||
private float mFadeTime = 300;
|
||||
private float mAlpha = 1;
|
||||
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent e, MapView mapView) {
|
||||
int action = e.getAction() & MotionEvent.ACTION_MASK;
|
||||
if (action == MotionEvent.ACTION_POINTER_DOWN) {
|
||||
multi++;
|
||||
} else if (action == MotionEvent.ACTION_POINTER_UP) {
|
||||
multi--;
|
||||
if (mPrevZoom != 17 && mAlpha > 0) {
|
||||
// finish hiding
|
||||
//Log.d(TAG, "add multi hide timer " + mAlpha);
|
||||
addShowTimer(mFadeTime * mAlpha, false);
|
||||
}
|
||||
} else if (action == MotionEvent.ACTION_CANCEL) {
|
||||
multi = 0;
|
||||
Log.d(TAG, "cancel " + multi);
|
||||
mTimer.cancel();
|
||||
mTimer = null;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private byte mPrevZoom = 0;
|
||||
|
||||
@Override
|
||||
public void onUpdate(MapPosition mapPosition, boolean changed) {
|
||||
byte z = mapPosition.zoomLevel;
|
||||
if (z == mPrevZoom)
|
||||
return;
|
||||
|
||||
if (z == 17) {
|
||||
// start showing
|
||||
//Log.d(TAG, "add show timer " + mAlpha);
|
||||
addShowTimer(mFadeTime * (1 - mAlpha), true);
|
||||
} else if (mPrevZoom == 17) {
|
||||
// indicate hiding
|
||||
if (multi > 0) {
|
||||
//Log.d(TAG, "add fade timer " + mAlpha);
|
||||
addFadeTimer(mFadeTime * mAlpha, false);
|
||||
} else {
|
||||
//Log.d(TAG, "add hide timer " + mAlpha);
|
||||
addShowTimer(mFadeTime * mAlpha, false);
|
||||
}
|
||||
}
|
||||
|
||||
mPrevZoom = z;
|
||||
}
|
||||
|
||||
void fade(float duration, long tick, boolean dir, float max) {
|
||||
|
||||
float a;
|
||||
if (dir)
|
||||
a = (1 - max) + (1 - (tick / duration)) * max;
|
||||
else
|
||||
a = (1 - max) + (tick / duration) * max;
|
||||
|
||||
Log.d(TAG, "fade " + dir + " " + tick + "\t" + a);
|
||||
|
||||
mAlpha = a;
|
||||
mExtLayer.setAlpha(a);
|
||||
mMapView.render();
|
||||
}
|
||||
|
||||
/* package */CountDownTimer mTimer;
|
||||
|
||||
private void addFadeTimer(final float ms, final boolean dir) {
|
||||
if (mTimer != null)
|
||||
mTimer.cancel();
|
||||
|
||||
mTimer = new CountDownTimer((long) ms, 16) {
|
||||
@Override
|
||||
public void onTick(long tick) {
|
||||
fade(ms, tick, dir, 0.2f);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFinish() {
|
||||
fade(ms, 0, dir, 0.2f);
|
||||
mTimer = null;
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
|
||||
private void addShowTimer(final float ms, final boolean dir) {
|
||||
final float d = mFadeTime;
|
||||
if (mTimer != null)
|
||||
mTimer.cancel();
|
||||
|
||||
mTimer = new CountDownTimer((long) ms, 16) {
|
||||
@Override
|
||||
public void onTick(long tick) {
|
||||
fade(d, tick, dir, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFinish() {
|
||||
fade(d, 0, dir, 1);
|
||||
mTimer = null;
|
||||
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user