【问题标题】:Is it possible to draw View or ViewGroup inside GLSurfaceView是否可以在 GLSurfaceView 内绘制 View 或 ViewGroup
【发布时间】:2012-10-24 16:58:18
【问题描述】:

是否可以在 GLSurfaceView 或 SurfaceView 的有界区域内绘制任何视图?

目前我的 GLSurfaceView 充满了位图纹理。

我想实现这样的目标

   +---------------------------------+
   |                                 |
   |         GLSurfaceView           |
   |                                 |
   |        +-------------+          |
   |        |WhateverView/|          |
   |        | Layout/     |          |
   |        |ViewGroup    |          |
   |        +-------------+          |
   |                                 |
   +---------------------------------+

我尝试使用 FrameLayout 在 GLSurfaceview 顶部绘制视图,但这不是我想要实现的目标。

谢谢。

【问题讨论】:

    标签: android view surfaceview viewgroup glsurfaceview


    【解决方案1】:

    尝试创建自己的扩展 GLSurfaceView 的类,覆盖方法 draw(Canvas) 并使用视图/视图组的方法 draw(Canvas) 在其中绘制视图。会是这样的:

    public class MyGLSurfaceView extends GLSurfaceView
    {
        private View myView;
    
        @Override
        protected void onDraw(Canvas canvas) {
            // Draw something below your view
            // Translate, rotate your canvas as you want
            myView.draw(canvas);
            canvas.restore(); // If you have translated or rotated the canvas
            // Draw something above your view
        }
    }
    

    【讨论】:

    • 实际上我的问题是如何绘制/绑定(如果可能的话)视图而不是该 GLSurfaceView 的该区域上的位图。
    • 我认为您可以将您的 GLSurfaceView 和您的 View 放在一个 RelativeLayout 中,并使用 GLSurfaceView 填充它并使用边距将 View 放置在您想要的位置。
    【解决方案2】:

    是的,可以,因为 GLSurfaceView 是 View 之一,你可以将它与其他 View 绑定到一个布局中。但请记住将 setZOrderOnTop() 设置为 false。

    请记住,Android 2.x 中的 OpenGL 不如 4.x 中的好(这意味着您必须解决 2.x 中发生的一些问题)

    【讨论】:

      【解决方案3】:

      我也有同样的问题。我在下面使用了这个技巧。

      使用AbsoluteLayout 重叠GLSurfaceView 和其他View

      AbsouluteLayout -+-- GLSurfaceView
                       +-- View
      

      设置GLSurfaceView透明。

      mGlSurfaceView.setEGLContextClientVersion(2);
      mGlSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
      mGlSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
      mGlSurfaceView.setRenderer(new GLES20TriangleRenderer(this));
      mGlSurfaceView.setZOrderOnTop(true);
      

      添加一个打孔。不要在GLSurfaceView 中渲染重叠区域。附加到 GLSurfaceViewOnTouchListener 在重叠区域返回 false 以将事件传播到其他视图。

      PS:对不起我的英语不好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-04
        • 1970-01-01
        • 2017-11-29
        相关资源
        最近更新 更多