sar*_*lla 11 android opengl-es surfaceview glsurfaceview
我们一直在与SurfaceViews相关的一些问题进行斗争一个多星期,并找不到合适的解决方案.我们在论坛中阅读了有关类似问题的其他问题(甚至是Mixare源代码)但找不到答案,所以我们希望您能以某种方式帮助我们.
场景:我们有
问题:
无论我们如何努力,两个SurfaceViews显然都不能相处得很好.如果我们试图:
setContentView(mCameraPreview);
addContentView(mGLSurfaceView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addContentView(mInfoView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Run Code Online (Sandbox Code Playgroud)
(这似乎合乎逻辑),一切都按预期进行,直到我们锁定/解锁手机.之后,GLSurfaceView就会消失(不是InfoView,仍会显示).
相反,如果我们尝试:
setContentView(mGLSurfaceView);
addContentView(mCameraPreview, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addContentView(mInfoView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Run Code Online (Sandbox Code Playgroud)
然后问题是GLSurfaceView仅在锁定/解锁后出现,在此之前屏幕显示相机和InfoView.
我们发现,如果我们在显示视图的活动中执行onStart()之后将主线程休眠4.6秒(或更多),则行为与预期一致(即使在锁定/解锁后,也会显示相机,glsurface和info视图).
问题是,我们正在寻找更优雅的解决方案.
在我们看来,问题是相机花费的时间比预期的多Camera.open(),因此添加了相机视图,添加了GLSurfaceView,当相机实际打开时,它会在GLSurfaceView的顶部打开.关于这一点,我们bringToFront()在GLSurfaceView上使用了它,并将其放在信息视图的顶部,但是在锁定/解锁后,相机仍然在其上面打开,只留下一个带有摄像头预览的屏幕.
有任何想法吗?我们如何在它们之上显示SurfaceViews和信息视图?
试试这个:
mLayout.addView(mRenderView);
mLayout.addView(mCustomSurfaceView);
// without this line, the camera preview cannot be displayed
// when running activity at first time.
mCustomSurfaceView.setZOrderMediaOverlay(true);
Run Code Online (Sandbox Code Playgroud)
这对我有用:)
我有同样的问题。正如您暗示自己的那样:多个SurfaceViews 彼此无法相处,因为它们的 Z 顺序未定义。
在我的三星 Galaxy S2 上,排序与您描述的相同(不知道其他手机上的情况如何)。我解决这个问题的方法是检查Activityin的首次创建onCreate():
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//...
if ( savedInstanceState == null )
{
initCamView();
initOpenGL();
}
else
{
initOpenGL();
initCamView();
}
//...
}
Run Code Online (Sandbox Code Playgroud)
和:
private void initOpenGL()
{
mGLSurfaceView = new GLSurfaceView(this);
mGLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
mGLSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
mOGLRenderer = new OGLRenderer(this);
mGLSurfaceView.setRenderer(mOGLRenderer);
mRL.addView(mGLSurfaceView); // mRL is the Relative Layout
}
Run Code Online (Sandbox Code Playgroud)
和:
private void initCamView()
{
mCamView = new CustomCameraView( this.getApplicationContext(),
this.getWindowManager() );
mRL.addView(mCamView); // mRL is the Relative Layout
}
Run Code Online (Sandbox Code Playgroud)
这不是最优雅的解决方案,但它比让线程休眠 4.6 秒要好。
也可以将屏幕锁定在单一方向,但随后您需要进行大量丑陋的黑客操作才能使覆盖层以正确的方式旋转。
或者,如果您仅针对 Android 3.0 或更高版本(API 级别 8),则可以仅在 OpenGL SurfaceTexture 中显示相机。http://developer.android.com/reference/android/hardware/Camera.html#setPreviewTexture(android.graphics.SurfaceTexture )
| 归档时间: |
|
| 查看次数: |
6938 次 |
| 最近记录: |