Skip to content Skip to sidebar Skip to footer

Android Opengl Es 2: How To Use An Opengl Activity As A Fragment In The Main Activity

I am quite new to Android and OpenGL ES. I have to create a GUI in OpenGL and I would like to use it as a Fragment in the main activity. In order to learn how to do this, I tried 2

Solution 1:

If the developer tutorial is anything to go by, then the following setup would work:

Activity:

publicclassMainActivityextendsFragmentActivity
{
    @OverrideprotectedvoidonCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getSupportFragmentManager().addOnBackStackChangedListener(newOnBackStackChangedListener()
        {
            publicvoidonBackStackChanged()
            {
                intbackCount= getSupportFragmentManager().getBackStackEntryCount();
                if (backCount == 0)
                {
                    finish();
                }
            }
        });

        if (savedInstanceState == null)
        {
            getSupportFragmentManager().beginTransaction().add(R.id.main_container, newOpenGLFragment()).addToBackStack(null).commit();
        }
    }
}

Activity XML (activity_main.xml):

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Fragment:

publicclassOpenGLFragmentextendsFragment
{ 
    private GLSurfaceView mGLView;

    publicOpenGLFragment()
    {
        super();
    }

    @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        mGLView = newMyGLSurfaceView(this.getActivity()); //I believe you may also use getActivity().getApplicationContext();return mGLView;
    }
}

And I guess you need to make your own GLSurfaceView as the tutorial says:

class MyGLSurfaceView extends GLSurfaceView {

    public MyGLSurfaceView(Context context){
        super(context);
        setEGLContextClientVersion(2);    
        // Set the Renderer for drawing on the GLSurfaceViewsetRenderer(new MyRenderer());

    }
}

And as the tutorial says, make your renderer:

publicclassMyGLRendererimplementsGLSurfaceView.Renderer {

    publicvoidonSurfaceCreated(GL10 unused, EGLConfig config) {
        // Set the background frame colorGLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    }

    publicvoidonDrawFrame(GL10 unused) {
        // Redraw background colorGLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    }

    publicvoidonSurfaceChanged(GL10 unused, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
    }
}

Post a Comment for "Android Opengl Es 2: How To Use An Opengl Activity As A Fragment In The Main Activity"