简体   繁体   English

如何处理GLSurfaceView的onPause / onResume

[英]How to handle onPause / onResume for GLSurfaceView

When a GlSurfaceView is embedded in a layout, eg, 当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> 

Then when the layout is inflated it gets constructed automatically using the constructor with the signature: GLSurfaceView(Context context, AttributeSet attrs) . 然后,当布局膨胀时,它将使用带有签名的构造函数自动构造: GLSurfaceView(Context context,AttributeSet attrs) So it's not literally declared or instantiated in the Activity class. 因此,它不是在Activity类中逐字声明或实例化的。

The Android documentation says that the Activity's onPause/onResume must call the SurfaceView's onPause/onResume. Android文档说Activity的onPause / onResume必须调用SurfaceView的onPause / onResume。 How should I do this? 我该怎么做? Ie, how can the Activity which inflated the layout get access to the GlSurfaceView object to make those calls? 即,夸大布局的Activity如何能够访问GlSurfaceView对象来进行这些调用?

Edit: This is for Android 2.2 编辑:这适用于Android 2.2

Thanks in advance! 提前致谢!

In your XML layout, give your SurfaceView a name by adding the name attribute: 在XML布局中,通过添加name属性为SurfaceView命名:

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

Next, override onPause and onResume in your activity, find the view by using findViewById(R.id.my_surfaceView1); 接下来,覆盖活动中的onPause和onResume,使用findViewById(R.id.my_surfaceView1);查找视图findViewById(R.id.my_surfaceView1); and then call onPause and onResume on your surfaceView: 然后在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();

}

Finally, in your implementation of your surface view, override onPause() / onResume() and put any code you need to do when your activity pauses / resumes in there. 最后,在您的表面视图的实现中,覆盖onPause()/ onResume()并在您的活动暂停/恢复时放置您需要执行的任何代码。 Remember also to call super.onPause() / super.onResume() in the surface view 还要记得在曲面视图中调用super.onPause()/ super.onResume()


Edit: just to clarify, you can use findViewById() method on any ViewGroup object to find child views inside that viewgroup: 编辑:只是为了澄清,你可以在任何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); } } 

Or findViewById() is also a method in Activity, which will find any view in the layout you set using setContentView(); 或者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.

相关问题 Android 游戏 - 如果游戏在 GLSurfaceView 的视频线程中运行,如何正确处理 onPause 和 onResume - Android Game - how correctly handle onPause and onResume if game runs in GLSurfaceView's video thread GLSurfaceView生命周期方法onPause()和onResume() - GLSurfaceView lifecycle methods onPause() and onResume() 如何在Flutter App中处理onPause / onResume? - How to handle onPause/onResume in Flutter App? 如何在OnPause和OnResume状态下处理ToggleButton状态 - How to handle ToggleButton state in OnPause and OnResume state Android - 如何正确处理onPause / onResume方法? - Android — How to properly handle onPause/onResume methods? GLSurfaceView:我需要调用onPause / onResume吗? - GLSurfaceView: Do I need to to call onPause/onResume? 何时调用GLSurfaceView方法onPause()和onResume() - When to call GLSurfaceView methods onPause() and onResume() 如何在onResume中处理LiveData项目 - 仅限onPause状态? - How to handle LiveData items in onResume - onPause state only? 如何在onPause和onResume上对其静音和取消静音 - How to mute and unmute it on the onPause and onResume 如何使用onpause和onresume创建活动来浏览URL? - How to create activity for browse a url with onpause and onresume?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM