如何在Android中的MapView上叠加GLSurfaceView?

时间:2010-05-13 09:10:30

标签: android opengl-es

我想在android中创建一个简单的基于Map的应用程序,我可以在其中显示我当前的位置。而不是在MapView上叠加一个简单的Image来表示位置,我想在MapView上叠加GLSurfaceView。但我不知道如何实现这一目标。有没有办法做到这一点?请有人知道解决方案对我有帮助。

2 个答案:

答案 0 :(得分:4)

在我查看文档并意识到MapView扩展ViewGroup之前,我遇到了类似的问题,因此覆盖GlSurfaceView实际上最终会变得非常微不足道。

MapView.addView(...)MapView.LayoutParams允许您进行一些非常有用的操作,例如将View放置在地图上的特定GeoPoint

更多文档:https://developers.google.com/maps/documentation/android/reference/com/google/android/maps/MapView.LayoutParams.html

这就是我的所作所为:

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mapview);

    MapView map_view = (MapView) findViewById(R.id.map_view);

    CustomGlView gl_view = new CustomGlView(this);

    // This is where the action happens.
    map_view.addView(gl_view, new MapView.LayoutParams(
            MapView.LayoutParams.FILL_PARENT, 
            MapView.LayoutParams.FILL_PARENT, 
            0,0, 
            MapView.LayoutParams.TOP_LEFT
            )
        );
}

另外,还要执行上述解决方案。我对GlSurfaceView进行了细分,因此我看起来略有不同:

public CustomGlView(Context context) {
    super(context);
    YourCustomGlRenderer renderer = new YourCustomGlRenderer();
    setEGLConfigChooser(8, 8, 8, 8, 16, 0);
    setRenderer(renderer);
    getHolder().setFormat(PixelFormat.TRANSLUCENT);

    // Have to do this or else 
    // GlSurfaceView wont be transparent.
    setZOrderOnTop(true);  
}

答案 1 :(得分:3)

你需要创建GLSurfaceView并像这样设置它的EGLConfigChooser:

glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);

然后将持有人的格式设置为半透明

glSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);

并将GLSurfaceView和MapView添加到布局

setContentView(glSurfaceView);

addContentView(mapView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

最后在你的渲染器中,你需要设置清晰的颜色,使其透明

gl.glClearColor(0, 0, 0, 0);