D
D
Dmitry2015-08-17 17:07:22
Java
Dmitry, 2015-08-17 17:07:22

How to embed OpenGL ES into XML?

I did the animation using the View class, but the animation hung up for me, I was advised to use OpenGl ES. My animation is 1/4 of the screen and I inserted it via XML markup like this:

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


        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#bbf"
            android:layout_weight="1">
            .
            .
            .
        </ScrollView>

        <ru.dima_n.elem.Anim
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/game_view"
            android:layout_weight="2"
            android:onClick="OnClickAnim"
            android:background="#000"/>

    </LinearLayout>

I also tried to insert the OpenGl class but I had an error that the class is not view and cannot be displayed. I tried to do this: I insert the View class into XML and start the OpenGL animation
XML markup in it:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="horizontal">


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scrollView"
        android:layout_weight="1"
        android:background="#000"/>

    <ru.dima_n.opengl.Anim
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/game_view"
        android:layout_weight="2"
        android:background="#ff0360ff"

    />

</LinearLayout>

Anim class:
public class Anim extends View
{
    GLSurfaceView glSurfaceView;
    nRender renderer;

    public Anim(Context context, AttributeSet attrs)
    {
        super(context, attrs);

        try{	// пытаемся инициализировать OpenGL
            glSurfaceView = new GLSurfaceView(context);

            glSurfaceView.setEGLContextClientVersion(2);

            renderer = new nRender();

            glSurfaceView.setRenderer(renderer);

            glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY); 
           
            Log.e("удачно", "3456");

        }catch(RuntimeException e){
            Log.e("error","error");
        } 

    }
}

nRender class:
public class nRender implements GLSurfaceView.Renderer
{
    GLSurfaceView glSurfaceView;

    public nRender()
    {
    }

    public void onDrawFrame(GL10 glUnused) {
        Random rnd = new Random();

        GLES20.glClearColor(((float)rnd.nextInt(2)/2.0f), ((float)rnd.nextInt(2)/2.0f), ((float)rnd.nextInt(2)/2.0f), 1.0f);
        Log.e("test","color"); // этот лог не выводиться
        GLES20.glClear( GLES20.GL_COLOR_BUFFER_BIT ); 
    }

    public void onSurfaceChanged(GL10 glUnused, int width, int height) { 
        GLES20.glViewport(0, 0, width, height);
        
    }

    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) { 

    }
}

At startup, the application opens without errors, I get a log of successful OpenGL initialization and that’s it, the animation does not occur, and I don’t get logs from the nRender class.
Please let me know what could be wrong, or maybe I'm not doing it right at all.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Perelygin, 2015-09-15
@orcDamnar

Well, initially it is not true that you are trying to initialize SurfaceView in the view constructor and thus receive an object, but do not receive it in your view hierarchy. To implement your problem, you can use GLSurfaceView or SurfaceView but above view should be, for example, inherited from GLSurfaceView. Learn more developer.android.com/reference/android/opengl/GLS...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question