2011-12-13 89 views
0

當GlSurfaceView被嵌入在佈局,例如,如何處理的onPause /的onResume爲GLSurfaceView

<FrameLayout 
    android:id="@+id/framelay" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <com.nelsondev.myha3ogl.M3View 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/> 
    </FrameLayout> 

然後,當佈局被充氣它得到自動使用與簽名構造構成:GLSurfaceView(上下文上下文,AttributeSet attrs)。所以它不是在Activity類中直接聲明或實例化的。

Android文檔指出Activity的onPause/onResume必須調用SurfaceView的onPause/onResume。我應該怎麼做?也就是說,膨脹佈局的Activity如何訪問GlSurfaceView對象來完成這些調用?

編輯:這是Android 2.2

提前

謝謝!

回答

2

在你的XML佈局,給你的SurfaceView一個名字加入name屬性:

<com.nelsondev.myha3ogl.M3View 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/my_surfaceView1"/> 

接下來,覆蓋的onPause和的onResume在活動中,通過使用findViewById(R.id.my_surfaceView1);找到視圖,然後調用的onPause和的onResume您surfaceView:

@Override 
public void onPause(){ 
    com.nelsondev.myha3ogl.M3View myView = (com.nelsondev.myha3ogl.M3View)findViewById(R.id.my_surfaceView1); 

    myView.onPause(); 

    super.onPause(); 

} 

最後,在你實現你的面來看,覆蓋的onPause()/的onResume(),把你需要做的任何代碼,當你的活動暫停/在那裏重新開始。也請記住調用super.onPause()/ super.onResume()在表面觀


編輯:我只想澄清,你可以使用任何的ViewGroup對象 findViewById()方法來發現裏面的ViewGroup子視圖:

MyActivity extends Activity{ 

    public void onCreate(Bundle bundle){ 

     FrameLayout myFrameLayout = (FrameLayout)getLayoutInflater().inflate(R.layout.graphics, null, false); 
     TextView myView = (TextView)myFrameLayout.findViewById(R.id.textView1); 

     if(myView!=null){ 
      myView.setText("about to be removed"); 
      myFrameLayout.removeView(myView); 
     } 

     setContentView(myFrameLayout); 


    } 
} 

或者findViewById()也是活動的方法,它會發現在任何佈局視圖中,您設置使用setContentView();

MyActivity extends Activity{ 

    public void onCreate(Bundle bundle){ 
     setContentView(R.layout.graphics); 
     // where the xml file in your question is called graphics.xml  
     com.nelsondev.myha3ogl.M3View myGLSurfaceView = (com.nelsondev.myha3ogl.M3View)findViewById(R.id.my_surfaceView1); 
     FrameLayout myFrameLayout = (FrameLayout)findViewById(R.id.framelay); 
    } 
} 
+0

謝謝!我多次使用findViewById來獲取按鈕和編輯控件等,但是我沒有意識到我可以像這樣使用自定義類。它完美運作;再次感謝! –

相關問題