Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the GLSurfaceView.Renderer class to show the object into surfaceView or the FrameLayout?

Today, I've made a 3D object with OpenGL ES in android and thinking of displaying it into other layouts such as SurfaceView or FrameLayout in xml or in any possible way.

In the code below I'm setting the GL object into the onCreate's setContentView to display my object. If I were to display this GLSurfaceView somewhere else how can I do it ? It would be great if I can have some tips or example!

    GLSurfaceView ourSurface;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        //setting the gl surface view
        ourSurface = new GLSurfaceView(this);
        ourSurface.setRenderer(new GLCubeRender());


        setContentView(ourSurface);

    }
like image 756
Jennifer Avatar asked Nov 16 '25 18:11

Jennifer


1 Answers

First you should create a class that extends GLSurfaceView with the constructor as you see in the following example

package com.ball.views;

import android.content.Context;
import android.opengl.GLSurfaceView; 
import android.util.AttributeSet;

public class MYGLSurfaceView extends GLSurfaceView {

   public MYGLSurfaceView(Context context, AttributeSet attrs) {
      super(context, attrs);
   }
}

After this you could create a xml file with your own GLSurfaceView inside

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

   <com.ball.views.MYGLSurfaceView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/GLSurfaceView1" />


</LinearLayout>

Then change your code you posted before in order to set in the view the xml file you have created and to get the GLSurfaceView from the xml file

MYGLSurfaceView ourSurface;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.your_xml_file);
    //setting the gl surface view
    ourSurface = (MYGLSurfaceView)this.findViewById(R.id.GLSurfaceView1);
    ourSurface.setRenderer(new GLCubeRender());

}
like image 196
Led Machine Avatar answered Nov 18 '25 10:11

Led Machine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!