簡體   English   中英

如何處理GLSurfaceView的onPause / onResume

[英]How to handle onPause / onResume for GLSurfaceView

當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(Context context,AttributeSet attrs) 因此,它不是在Activity類中逐字聲明或實例化的。

Android文檔說Activity的onPause / onResume必須調用SurfaceView的onPause / onResume。 我該怎么做? 即,誇大布局的Activity如何能夠訪問GlSurfaceView對象來進行這些調用?

編輯:這適用於Android 2.2

提前致謝!

在XML布局中,通過添加name屬性為SurfaceView命名:

<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);查找視圖findViewById(R.id.my_surfaceView1); 然后在surfaceView上調用onPause和onResume:

@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()方法來查找該視圖組內的子視圖:

 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()也是Activity中的一個方法,它可以在你使用setContentView();設置的布局中找到任何視圖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); } } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM