繁体   English   中英

如何将GLSurfaceView放在android上的布局中

[英]How to put GLSurfaceView inside a layout on android

我想创建一个应用程序,其中glsurfaceview与listview一起位于相对布局中。 编码时,我的IDE没有显示任何错误,但是一旦我运行应用程序,它就会崩溃。

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="horizontal">

    <ListView
        android:layout_height="wrap_content"
        android:layout_width="100dp"
        android:background="#6D6D6D"
        android:id="@+id/listObjects"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_centerHorizontal="false"/> 

    <com.shirofuji.thrrededit.GL_handler
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/gl_layout"
        android:layout_alignParentLeft="true"
        android:layout_alignEnd="@id/listObjects"/>

</RelativeLayout>

主要活动

package com.shirofuji.thrrededit;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.opengl.*;

public class MainActivity extends Activity
{
    GL_handler gl_maineditor;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        gl_maineditor = (GL_handler)findViewById(R.id.gl_layout);
        //layout_3d.addView(gl_maineditor);
        setContentView(R.layout.main);
    }

@Override
public void onResume(){
    super.onResume();
    gl_maineditor.onResume();
}

@Override
public void onPause(){
    super.onPause();
    gl_maineditor.onPause();
    }
}

GL_handler

package com.shirofuji.thrrededit;
import android.opengl.*;
import android.content.Context;
import android.view.View;
import android.util.*;

public class GL_handler extends View
{
    GLSurfaceView mainsurface;
    GL_renderer scene_renderer;

    public GL_handler(Context ctx,AttributeSet attr){
        super(ctx,attr);
        mainsurface = new GLSurfaceView(ctx);
        scene_renderer = new GL_renderer();

        mainsurface.setRenderer(scene_renderer);
    }
    public void onResume(){
        mainsurface.onResume();
    }
    public void onPause(){
        mainsurface.onPause();
    }
}

活动的onCreate()方法中的调用顺序是错误的:

gl_maineditor = (GL_handler)findViewById(R.id.gl_layout);
setContentView(R.layout.main);

在使用findByViewId()获取任何视图之前,需要调用setContentView() findByViewId() 顺序应该是:

setContentView(R.layout.main);
gl_maineditor = (GL_handler)findViewById(R.id.gl_layout);

除此之外,我认为还有另一个方面,你可能会走错路。 除非您的主计划在发布的代码中不可见。 您没有在布局中放置GLSurfaceView 放置在布局中的GL_handler源自普通View 它有一个GLSurfaceView作为成员变量,但仅此一项不会使GLSurfaceView出现。

除非你有充分的理由,否则从GLSurfaceView派生你的GL_handler类要容易GLSurfaceView 这实际上会导致GLSurfaceView在布局膨胀时成为视图层次结构的一部分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM