简体   繁体   中英

Android SurfaceView overlay on camera view does not show when preview is on

I have a FrameLayout containing two SurfaceViews. One of them will show the camera preview, and the other is a custom surface view I use for drawing. Problem is the surface view does not show up. If I comment out the line where the camera starts preview, it does though, on the black background. This indicates me that the Surface view is positioned well, but I suspect that when the camera preview draws, something goes wrong.

The camera view:

public CameraView(Context context, AttributeSet a) {
    super(context, a);
    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

The custom view:

public InclinaisonGauge(Context context, AttributeSet attrs) {
    super(context, attrs);
    getHolder().addCallback(this);
    getHolder().setFormat(PixelFormat.TRANSLUCENT);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    run = true;
    updateThread = new Thread()
    {
        public void run()
        {
            Canvas c = null;
            final SurfaceHolder inclHolder = getHolder();
            while (run) {
                try {
                    c = inclHolder.lockCanvas();
                    if(c!=null)
                    {
                        synchronized (inclHolder) {
                            onDraw(c);
                        }
                    }
                } finally {
                // do this in a finally so that if an exception is thrown
                // during the above, we don't leave the Surface in an
                // inconsistent state
                if (c != null) {
                    inclHolder.unlockCanvasAndPost(c);
                }
            }           
        }
    }
};

And I use this very simple draw method on the custom view to make sure it is not a framerate issue

public void doDraw(final Canvas canvas, final boolean bigSize, float daDirection)
{
    canvas.drawARGB(255, 255, 255, 255);
}

Has anyone experienced this?

Thanks

好吧,如果有其他人得到这个,解决方法是在surfaceView上调用它

this.setZOrderMediaOverlay(true);

I don't think SurfaceViews are intended to be used as an overlay, but it is possible to achieve this behavior. What you have to do is when the SurfaceView class is created:

    setZOrderMediaOverlay(true);
    getHolder().setFormat(PixelFormat.TRANSPARENT);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM