[英]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()
findViewById()
method on any ViewGroup object to find child views inside that 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.