How to modify the Android OpenGL ES 1.0 tutorial to use XML and a framelayout

crozzbreed23

New Member
The OpenGL ES 1.0 tutorial listed here (http://developer.android.com/resources/tutorials/opengl/opengl-es10.html) works well but I want to make it work with multiple layers and an XML Framelayout. So essentially I would have something like this:-class A extends Activity-class B extends GLSurfaceView-class C implements GLSurfaceView.RendererThe original example has class B handling touch events and class C rendering the object to be displayed. I want to extend this further by adding additional geometry layers. I have it working with a camera layout (although it crashes when changing orientation). Working Version (Main.java):\[code\]public class Main extends Activity { MainSurfaceView mGLView;CameraView cameraView;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Set full screen requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); mGLView = new MainSurfaceView(this, null); setContentView(mGLView); cameraView = new CameraView(this, null); addContentView(cameraView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));}\[/code\]Non Working That I want to fix (Main.java):\[code\]public class Main extends Activity { MainSurfaceView mGLView;CameraView cameraView;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Set full screen requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main); mGLView = (MainSurfaceView) findViewById(R.id.glSurfaceView) cameraView = (CameraView) findViewById(R.id.camera);}\[/code\]The Custom view class (MainSurfaceView.java(\[code\]public class MainSurfaceView extends GLSurfaceView {private final float TOUCH_SCALE_FACTOR = 180.0f / 320;private MainRenderer mRenderer;private float mPreviousX;private float mPreviousY;public MainSurfaceView(Context context, AttributeSet attrs) { super(context, attrs); mRenderer = new MainRenderer(); setEGLConfigChooser(8, 8, 8, 8, 16, 0); setRenderer(mRenderer); getHolder().setFormat(PixelFormat.TRANSLUCENT); setZOrderOnTop(true); //Render only when there is a change setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); } ...\[/code\]The non working code is crashing due to an unhandled exception? Why is that when I handle the eception in the CameraView.java\[code\]... try { camera.setPreviewDisplay( holder ); } catch( IOException e ) { e.printStackTrace(); } // ...and start previewing. From now on, the camera keeps pushing preview // images to the surface. camera.startPreview();\[/code\]The XML file (main.xml)\[code\] <?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <android.opengl.GLSurfaceView android:id="@+id/glSurfaceView" android:layout_width="match_parent" android:layout_height="match_parent" /> <SurfaceView android:id="@+id/camera" android:layout_width="match_parent" android:layout_height="match_parent" android:keepScreenOn="true" /></FrameLayout>\[/code\]Full logcat trace around exception06-05 12:34:40.386: D/AndroidRuntime(282): Shutting down VM06-05 12:34:40.396: W/dalvikvm(282): threadid=1: thread exiting with uncaught exception (group=0x4001d800)06-05 12:34:40.466: E/AndroidRuntime(282): FATAL EXCEPTION: main06-05 12:34:40.466: E/AndroidRuntime(282): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zypath.opengltest/com.zypath.opengltest.Main}: java.lang.ClassCastException: android.opengl.GLSurfaceView06-05 12:34:40.466: E/AndroidRuntime(282): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)06-05 12:34:40.466: E/AndroidRuntime(282): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)06-05 12:34:40.466: E/AndroidRuntime(282): at android.app.ActivityThread.access$2300(ActivityThread.java:125)06-05 12:34:40.466: E/AndroidRuntime(282): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)06-05 12:34:40.466: E/AndroidRuntime(282): at android.os.Handler.dispatchMessage(Handler.java:99)06-05 12:34:40.466: E/AndroidRuntime(282): at android.os.Looper.loop(Looper.java:123)06-05 12:34:40.466: E/AndroidRuntime(282): at android.app.ActivityThread.main(ActivityThread.java:4627)06-05 12:34:40.466: E/AndroidRuntime(282): at java.lang.reflect.Method.invokeNative(Native Method)06-05 12:34:40.466: E/AndroidRuntime(282): at java.lang.reflect.Method.invoke(Method.java:521)06-05 12:34:40.466: E/AndroidRuntime(282): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)06-05 12:34:40.466: E/AndroidRuntime(282): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)06-05 12:34:40.466: E/AndroidRuntime(282): at dalvik.system.NativeStart.main(Native Method)06-05 12:34:40.466: E/AndroidRuntime(282): Caused by: java.lang.ClassCastException: android.opengl.GLSurfaceView06-05 12:34:40.466: E/AndroidRuntime(282): at com.zypath.opengltest.Main.onCreate(Main.java:33)06-05 12:34:40.466: E/AndroidRuntime(282): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)06-05 12:34:40.466: E/AndroidRuntime(282): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)06-05 12:34:40.466: E/AndroidRuntime(282): ... 11 more06-05 12:34:42.806: I/Process(282): Sending signal. PID: 282 SIG: 9I have been through several examples and that's where I got the MainSurfaceView constructor(Context context, AttributeSet attrs) to use with the XML views. What am I doing wrong here??Fixed it I had forgot to refer to the custom view class and camera class in my xml file. The correct XML file looks like this:\[code\]<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.company.opengltest.MainSurfaceView android:id="@+id/glSurfaceView" android:layout_width="match_parent" android:layout_height="match_parent" /> <com.company.opengltest.CameraView android:id="@+id/camera" android:layout_width="match_parent" android:layout_height="match_parent" android:keepScreenOn="true" /></FrameLayout>\[/code\]
 
Back
Top