android: refactor MapView
- merge AndroidMap + GLView into Mapview - let MapView directly extends GLSurfaceView
This commit is contained in:
@@ -16,8 +16,12 @@
|
||||
*/
|
||||
package org.oscim.android;
|
||||
|
||||
import javax.microedition.khronos.egl.EGLConfig;
|
||||
import javax.microedition.khronos.opengles.GL10;
|
||||
|
||||
import org.oscim.android.canvas.AndroidGraphics;
|
||||
import org.oscim.android.gl.AndroidGL;
|
||||
import org.oscim.android.gl.GlConfigChooser;
|
||||
import org.oscim.android.input.AndroidMotionEvent;
|
||||
import org.oscim.android.input.GestureHandler;
|
||||
import org.oscim.backend.CanvasAdapter;
|
||||
@@ -28,19 +32,26 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.opengl.GLSurfaceView;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.view.GestureDetector;
|
||||
import android.widget.RelativeLayout;
|
||||
|
||||
public class MapView extends RelativeLayout {
|
||||
/**
|
||||
* The MapView,
|
||||
*
|
||||
* add it your App, have a map!
|
||||
*
|
||||
* Dont forget to call onPause / onResume!
|
||||
*/
|
||||
public class MapView extends GLSurfaceView {
|
||||
|
||||
static final Logger log = LoggerFactory.getLogger(MapView.class);
|
||||
|
||||
static {
|
||||
System.loadLibrary("vtm-jni");
|
||||
}
|
||||
|
||||
static final Logger log = LoggerFactory.getLogger(MapView.class);
|
||||
|
||||
protected final AndroidMap mMap;
|
||||
protected final GestureDetector mGestureDetector;
|
||||
protected final AndroidMotionEvent mMotionEvent;
|
||||
@@ -49,12 +60,17 @@ public class MapView extends RelativeLayout {
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public MapView(Context context, AttributeSet attributeSet) {
|
||||
super(context, attributeSet);
|
||||
|
||||
/* Not sure if this makes sense */
|
||||
this.setWillNotDraw(true);
|
||||
this.setClickable(true);
|
||||
this.setFocusable(true);
|
||||
this.setFocusableInTouchMode(true);
|
||||
|
||||
/* Setup android backedn */
|
||||
AndroidGraphics.init();
|
||||
AndroidAssets.init(context);
|
||||
GLAdapter.init(new AndroidGL());
|
||||
@@ -62,8 +78,21 @@ public class MapView extends RelativeLayout {
|
||||
DisplayMetrics metrics = getResources().getDisplayMetrics();
|
||||
CanvasAdapter.dpi = (int) Math.max(metrics.xdpi, metrics.ydpi);
|
||||
|
||||
/* Initialize the Map */
|
||||
mMap = new AndroidMap(this);
|
||||
|
||||
/* Initialize Renderer */
|
||||
setEGLConfigChooser(new GlConfigChooser());
|
||||
setEGLContextClientVersion(2);
|
||||
|
||||
if (GLAdapter.debug)
|
||||
setDebugFlags(GLSurfaceView.DEBUG_CHECK_GL_ERROR
|
||||
| GLSurfaceView.DEBUG_LOG_GL_CALLS);
|
||||
|
||||
setRenderer(new GLRenderer(mMap));
|
||||
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
|
||||
|
||||
/* to be removed */
|
||||
if (context instanceof MapActivity)
|
||||
((MapActivity) context).registerMapView(this);
|
||||
|
||||
@@ -81,11 +110,11 @@ public class MapView extends RelativeLayout {
|
||||
|
||||
}
|
||||
|
||||
void onPause() {
|
||||
public void onPause() {
|
||||
mMap.pause(true);
|
||||
}
|
||||
|
||||
void onResume() {
|
||||
public void onResume() {
|
||||
mMap.pause(false);
|
||||
}
|
||||
|
||||
@@ -116,4 +145,113 @@ public class MapView extends RelativeLayout {
|
||||
public Map map() {
|
||||
return mMap;
|
||||
}
|
||||
|
||||
static class AndroidMap extends Map {
|
||||
|
||||
private final MapView mMapView;
|
||||
|
||||
private boolean mRenderRequest;
|
||||
private boolean mRenderWait;
|
||||
private boolean mPausing;
|
||||
|
||||
public AndroidMap(MapView mapView) {
|
||||
super();
|
||||
mMapView = mapView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWidth() {
|
||||
return mMapView.getWidth();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight() {
|
||||
return mMapView.getHeight();
|
||||
}
|
||||
|
||||
private final Runnable mRedrawCb = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
prepareFrame();
|
||||
mMapView.requestRender();
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public void updateMap(boolean redraw) {
|
||||
if (mPausing)
|
||||
return;
|
||||
|
||||
if (!mRenderRequest) {
|
||||
mRenderRequest = true;
|
||||
mMapView.post(mRedrawCb);
|
||||
} else {
|
||||
mRenderWait = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
if (mPausing)
|
||||
return;
|
||||
|
||||
updateMap(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beginFrame() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doneFrame() {
|
||||
mRenderRequest = false;
|
||||
|
||||
if (mRenderWait) {
|
||||
//log.debug("redraw");
|
||||
mRenderWait = false;
|
||||
updateMap(true);
|
||||
//prepareFrame();
|
||||
//mGLView.requestRender();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean post(Runnable runnable) {
|
||||
return mMapView.post(runnable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean postDelayed(Runnable action, long delay) {
|
||||
return mMapView.postDelayed(action, delay);
|
||||
}
|
||||
|
||||
public void pause(boolean pause) {
|
||||
log.debug("pause... {}", pause);
|
||||
mPausing = pause;
|
||||
}
|
||||
}
|
||||
|
||||
static class GLRenderer extends org.oscim.renderer.MapRenderer
|
||||
implements GLSurfaceView.Renderer {
|
||||
|
||||
public GLRenderer(Map map) {
|
||||
super(map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
|
||||
super.onSurfaceCreated();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSurfaceChanged(GL10 gl, int width, int height) {
|
||||
super.onSurfaceChanged(width, height);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDrawFrame(GL10 gl) {
|
||||
super.onDrawFrame();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user