如何正确地在Android上使用setZOrderMediaOverlay? [英] How to properly use setZOrderMediaOverlay on Android?

查看:2994
本文介绍了如何正确地在Android上使用setZOrderMediaOverlay?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

和许多人一样,我想绘制3D对象(使用GLSurfaceView)相机preVIEW(使用SurfaceView),以及一些按钮放在上面。事实上,我的原型工作,但我无法得到onResume正常工作。后一份简历,GLSurfaceView留下的,是不可见的了。我知道这是工作,因为我可以看到图纸的罚款,如果我从布局中删除的SurfaceView。

Like many others, I am trying to draw 3D objects (using GLSurfaceView) on camera preview (using SurfaceView), along with some buttons placed on top. I actually got a prototype working, but I could not get the onResume working correctly. After a resume, GLSurfaceView stays behind and is not visible anymore. I know that it is working, because I can see the drawing fine if I remove the SurfaceView from the layout.

目标是Nexus One的,运行的股票2.3.3 ROM。

The target is Nexus One, running stock 2.3.3 ROM.

下面是布局的xml:

Here's the layout 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>

所有这一切都被包裹在一个&LT;合并&GT; 块,但由于某些原因,我不能将其包含在上述&LT; code&GT; 上市。 preVIEW 是一个扩展的类 SurfaceView 并实现了摄像头preVIEW逻辑。

All of this is wrapped in a <merge> block, but for some reason, I could not include it in the above <code> listing. Preview is the class that extends SurfaceView and implements the camera preview logic.

这工作正常时,活动启动。当我启动另一个活动(例如,通过pressing其中一个按钮),并完成该活动,主要活动恢复,但3D对象是不可见的了。

This works fine when the activity launches. After I launch another activity (for example, by pressing one of the buttons) and finish that activity, the main activity resumes but the 3D objects are not visible anymore.

我发现这个讨论在那里提到 setZOrderMediaOverlay( )将有助于解决这个z排序的问题。另外,在<一href="http://developer.android.com/reference/android/view/SurfaceView.html#setZOrderMediaOverlay%28boolean%29">Android对于setZOrderMediaOverlay()说,这种功能被称为前表面视图的包含窗口连接到窗口管理器的文件。我不知道我应该怎么跨preT这句话,所以我把调试语句在code看到事件的顺序。以下是如何 GLSurfaceView code是这样的:

I found this discussion where it is mentioned that setZOrderMediaOverlay() will help fix this z-ordering issue. Also, the Android document for setZOrderMediaOverlay() says this function should be called "before the surface view's containing window is attached to the window manager". I do not know how I should interpret that statement, so I put debug statements in the code to see the sequence of events. Here's how the GLSurfaceView code looks like:


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);
    }
}

下面的事件序列,当我启动应用程序:

Here's the sequence of events when I start the application:


GLPreview: OnResume
GLPreview: onAttachedToWindow
GLPreview: onWindowVisibilityChanged -> VISIBLE
GLPreview: surfaceCreated
GLPreview: surfaceChanged, format=1, w=800, h=480

所以,我认为 setZOrderMediaOverlay()需要调用在任 onResume()或在构造函数中。不过,我试过 setZOrderMediaOverlay()的各种组合与,在 GLSurfaceView SurfaceView 类,但是它没有一个帮助的问题。

So I assumed that setZOrderMediaOverlay() needs to be called in either onResume() or in the constructor. However, I tried various combinations of setZOrderMediaOverlay() with true or false, in the GLSurfaceView or the SurfaceView classes, but none of it helped with the problem.

我是比较新的,当涉及到Android编程。我已经走了这么远,但在这一点上我需要一些帮助。如果有人谁使用 setZOrderMediaOverlay()成功地在这种情况下,得到了其与 onResume()工作让我知道如何需要做。

I am relatively new when it comes to Android programming. I have come so far but at this point I need some help. If someone who used setZOrderMediaOverlay() successfully in this scenario and got it working with onResume() let me know how this needs to be done.

感谢很多提前!

推荐答案

我一直在努力与同样的问题了一段时间,但相信code低于现在的作品。 (它继续暂停/锁定无论相机是否开启或关闭后继续工作)。

I have struggled with the same issue for some time, but believe the code below now works. (It continues to work after pausing/ locking regardless of whether the camera is on or off).

首先,我没有在布局文件中定义的任何意见等 - 他们在code创建。下面被称为从主的onCreate()方法 - 请注意在augScreen的setZOrderMediaOverlay()

Firstly, I haven't got any views etc defined in the layout file - they're created in code. The following is called from the main onCreate() method - Note the setZOrderMediaOverlay() on the augScreen.

我除了通过传递的onPause()和onResume()到augScreen做什么特别的范围内的onPause()或onResume()。

I have done nothing special within onPause() or onResume() apart from passing onPause() and onResume() through to the augScreen.

        // 1 - Camera Preview
        camScreen = new CameraPreview(this);
        setContentView(camScreen, new LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    // 2 - 3D object display
        augScreen = new AR_SurfaceView(this, getViewRange());
        addContentView(augScreen, new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        augScreen.setZOrderMediaOverlay(true);

    // 3 - Permanent overlay
    RelativeLayout overlayScreen = new OverlayView(this);
    addContentView(overlayScreen, new LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    overlayScreen.setVisibility(View.VISIBLE);

    // 4 - UI buttons (toggleable)
        uiScreen = new UserInterfaceView(this);
        addContentView(uiScreen, new LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

这篇关于如何正确地在Android上使用setZOrderMediaOverlay?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆