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

3

我想在Android上创建一个简单的基于地图的应用程序,可以显示我的当前位置。但是,我不想在MapView上叠加简单的图像来表示位置,而是想在MapView上叠加GLSurfaceView。但我不知道如何实现这一点。有没有办法做到这一点?如果有人知道解决方法,请帮帮我。


嗨,Rajapandian,你找到了你的帖子的解决方案吗?因为我现在也处于同样的情况。你能帮帮我吗? - Rathakrishnan Ramasamy
2个回答

5

我曾经卡在类似的问题上,直到查看文档并意识到 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
            )
        );
}

除了上面的解决方案,Plus还可以做到这一点。 我对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);  
}

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);

我尝试了这种方法,但旋转没有应用。MapView保持稳定。 - kml_ckr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接