2011-12-18 52 views
0

我是Android上的OpenGL的新手,上週我描述了一個問題,無法讓渲染器在佈局中聲明的GLSurfaceView啓動。如果我在Activity類和setContentView中聲明渲染器,它會開始很好。這裏有一個簡化版,全部爲的源代碼。我究竟做錯了什麼?試圖從佈局中聲明的GLSurfaceView開始渲染器

佈局。 。 。

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

    <Button 
    android:id="@+id/dummy" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="a Button" /> 
    <FrameLayout 
    android:id="@+id/framelay" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <com.test.rendertest.RTSurface 
    android:id="@+id/RTSurfaceView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/> 
    </FrameLayout> 
</LinearLayout> 

Activity類。取消註釋// A's,並註釋掉// B並且渲染器運行。但是,如下所示,即使構造函數被調用,渲染器也不會運行。

public class RenderTest extends Activity { 

     RTSurface myView; 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
//  myView = new RTSurface(this); // A 
//  setContentView(myView);   //A 
      setContentView(R.layout.main);  // B 
      myView = (com.test.rendertest.RTSurface)findViewById(R.id.RTSurfaceView); //B 
     } 

     @Override 
     protected void onPause() { 
      super.onPause(); 
      myView.onPause(); 
     } 

     @Override 
     protected void onResume() { 
      super.onResume(); 
      myView.onResume(); 
     }  
} 

GLSurfaceView。 。 。

class RTSurface extends GLSurfaceView { 
    private final RTRenderer renderer; 

    public RTSurface(Context context) { 
     super(context); 
     Log.i("rendertest", "RTSurface constructor - Default Form"); 
     renderer = new RTRenderer(); 
     setRenderer(renderer); 
    } 

    public RTSurface(Context context, AttributeSet attrs) { 
     super(context, attrs);  
     Log.i("rendertest", "RTSurface constructor - Layout Form"); 
     renderer = new RTRenderer(); 
     setRenderer(renderer); 
    } 
} 

。 。 。和渲染(只是存根)

class RTRenderer implements GLSurfaceView.Renderer { 

    public RTRenderer() { 
     // a constructor just to have somewhere to set 
     // breakpoints and logcat messages 
     Log.i("rendertest", "RTRenderer Constructor");  
    } 

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {    
     Log.i("rendertest", "onSurfaceCreated in RTRenderer");     
    } 

    public void onSurfaceChanged(GL10 gl, int width, int height) { 
     Log.i("rendertest", "onSurfaceChanged in RTRenderer");     
    } 

    public void onDrawFrame(GL10 gl) { 
     Log.i("rendertest", "onDrawFrame in RTRenderer");  
    } 
} 

在此先感謝!

+1

我複製並粘貼代碼到項目,一切對我的作品...... – Jave 2011-12-18 22:08:11

+0

@Jave:怎麼可能呢? LadaRaider(以下)正確地確定了(非常真實的)問題。 – 2011-12-19 00:59:35

+0

哈哈,當然!我只複製了Java代碼並編寫了自己的佈局文件,因爲我不認爲這會是錯誤。現在看起來很明顯,我讀了它。 – Jave 2011-12-19 08:55:36

回答

2

您沒有指定您的LinearLayout方向,因此它默認設置爲horizontal。這意味着您的GLSurfaceView在屏幕之外(因爲您將按鈕寬度設置爲fill_parent)。

只需將以下屬性添加到您的LinearLayout:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
+0

我從來沒有想到表面不會被創建,或者渲染器不會僅僅因爲它不可見而運行!那個怎麼樣?!無論如何感謝您注意到這一點,如果你想拿起50點賞金,這是我的另一個問題的答案:http://stackoverflow.com/questions/8498877/glsurfaceview-renderer-not-being-called(我會將它標記爲僅適用於LadaRaider) – 2011-12-19 00:57:44

+0

謝謝,我也會回答你的其他問題。是的,這是一個棘手的問題,因爲如果一個視圖不可見,那麼它根本不會被繪製(以避免無用的函數調用)。 – Dalmas 2011-12-19 12:40:24