像许多其他人一样,我正在尝试在相机预览(使用 SurfaceView)上绘制 3D 对象(使用 GLSurfaceView),以及放置在顶部的一些按钮。我实际上得到了一个原型工作,但我无法让 onResume 正常工作。恢复后,GLSurfaceView 留在后面,不再可见。我知道它正在工作,因为如果我从布局中删除 SurfaceView,我可以看到很好的绘图。
目标是 Nexus One,运行库存 2.3.3 ROM。
这是布局xml:
<com.example.cameratest.GLPreview
android:id="@+id/cubes"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<com.example.cameratest.Preview
android:id="@+id/camera"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/buttongroup"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="right" android:gravity="center">
<Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:id="@+id/buttonClose"></Button>
<Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:id="@+id/buttonMode"></Button>
<Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:id="@+id/buttonCameraInfo"></Button>
</LinearLayout>
所有这些都包含在一个<merge>
块中,但由于某种原因,我无法将它包含在上面的<code>
清单中。Preview
是扩展SurfaceView
和实现相机预览逻辑的类。
这在活动启动时工作正常。在我启动另一个活动(例如,通过按下其中一个按钮)并完成该活动后,主要活动恢复,但 3D 对象不再可见。
我发现了这个讨论,其中提到这setZOrderMediaOverlay()
将有助于解决这个 z 排序问题。此外,setZOrderMediaOverlay() 的 Android 文档说,应该在“将表面视图的包含窗口附加到窗口管理器之前”调用此函数。我不知道我应该如何解释该语句,所以我将调试语句放在代码中以查看事件顺序。代码如下GLSurfaceView
所示:
public class GLPreview extends GLSurfaceView
{
private static final String TAG = "GLPreview";
public GLPreview(Context context, AttributeSet attrs)
{
super(context, attrs);
// this.setZOrderMediaOverlay(true);
}
public void onPause()
{
super.onPause();
Log.d(TAG, "GLPreview: OnPause");
}
public void onResume()
{
// this.setZOrderMediaOverlay(true);
super.onResume();
Log.d(TAG, "GLPreview: OnResume");
}
public void surfaceCreated(SurfaceHolder holder)
{
Log.d(TAG, "GLPreview: surfaceCreated");
super.surfaceCreated(holder);
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
{
Log.d(TAG, String.format("GLPreview: surfaceChanged, format=%d, w=%d, h=%d", format, w, h));
super.surfaceChanged(holder, format, w, h);
}
public void surfaceDestroyed(SurfaceHolder holder)
{
Log.d(TAG, "GLPreview: surfaceDestroyed");
super.surfaceDestroyed(holder);
}
protected void onAttachedToWindow()
{
Log.d(TAG, "GLPreview: onAttachedToWindow");
super.onAttachedToWindow();
}
protected void onDetachedFromWindow()
{
Log.d(TAG, "GLPreview: onDetachedFromWindow");
super.onDetachedFromWindow();
}
protected void onWindowVisibilityChanged (int vis)
{
String newVisibility;
switch (vis)
{
case View.GONE:
newVisibility = "GONE";
break;
case View.INVISIBLE:
newVisibility = "INVISIBLE";
break;
case View.VISIBLE:
newVisibility = "VISIBLE";
break;
default:
newVisibility = String.format("Unknown constant %d", vis);
}
Log.d(TAG, String.format("GLPreview: onWindowVisibilityChanged -> %s", newVisibility));
super.onWindowVisibilityChanged(vis);
}
}
这是我启动应用程序时的事件顺序:
GLPreview:OnResume GLPreview:onAttachedToWindow GLPreview:onWindowVisibilityChanged -> 可见 GLPreview:表面创建 GLPreview:surfaceChanged,格式=1,w=800,h=480
所以我假设setZOrderMediaOverlay()
需要onResume()
在构造函数中或构造函数中调用它。但是,我在or类中尝试了setZOrderMediaOverlay()
with true
or的各种组合,但都没有解决问题。false
GLSurfaceView
SurfaceView
在 Android 编程方面,我相对较新。我已经走了这么远,但在这一点上我需要一些帮助。如果有人setZOrderMediaOverlay()
在这种情况下成功使用并与之合作,onResume()
请告诉我这需要如何完成。
提前非常感谢!