簡體   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