开发者

OpenGL view inside a layout

开发者 https://www.devze.com 2023-01-24 05:06 出处:网络
How do I set up an xml layout where an OpenGL view is part of it?As I do now is set the OpenGL view as the only view with setContentView().But I would like to create an xml l开发者_JAVA技巧ayout that

How do I set up an xml layout where an OpenGL view is part of it? As I do now is set the OpenGL view as the only view with setContentView(). But I would like to create an xml l开发者_JAVA技巧ayout that includes the OpenGL view. Lets say I want to have the OpenGL view mainly and a small TextView at the bottom.

Is this even possible? Or can an OpenGL view only be the one and only view?


This is what I did for my Particle Emitter: extend a GLSurfaceView and make it part of my layout. Note: implement the "ParticleRenderer" class to achieve whatever openGL stuff you want to do

My Custom view:

public class OpenGLView extends GLSurfaceView
{

    //programmatic instantiation
    public OpenGLView(Context context)
    {
        this(context, null);
    }

    //XML inflation/instantiation
    public OpenGLView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public OpenGLView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs);

        // Tell EGL to use a ES 2.0 Context
        setEGLContextClientVersion(2);

        // Set the renderer
        setRenderer(new ParticleRenderer(context));
    }

}

and in the layout...

<com.hello.glworld.particlesystem.OpenGLView
    android:id="@+id/visualizer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

The particle Render is straight forward... for some sample code, see: https://code.google.com/p/opengles-book-samples/source/browse/trunk/Android/Ch13_ParticleSystem/src/com/openglesbook/particlesystem/ParticleSystemRenderer.java

public class ParticleRenderer implements GLSurfaceView.Renderer
{
    public ParticleRenderer(Context context)
    {
        mContext = context;
    }

    @Override
    public void onDrawFrame(GL10 gl)
    {
        //DO STUFF
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height)
    {
        //DO STUFF
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config)
    {
        //DO STUFF
    }
}


You might look at SurfaceView. It provides a dedicated drawing surface embedded inside of a view hierarchy. See also drawing with canvas.


Create a LinearLayout inside your xml file. Then in the activity, use findViewById() to get the layout and use addView() to add the OpenGL SurfaceView in your layout:

LinearLayout l = (LinearLayout) findViewById(R.id.MyLinearLayout);  
GLSurfaceView s = new GLSurfaceView(this);
s.setRenderer(myGLRenderer);

//to add the view with your own parameters
l.addView(s, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

//or simply use
l.addView(s,0);
0

精彩评论

暂无评论...
验证码 换一张
取 消