简体   繁体   English

如何在SurfaceView顶部使自定义叠加视图透明?

[英]How to Make a Custom Overlay View Transparent on top of a SurfaceView?

OK I've gone through many similar questions here on SO and around the web, to no avail. 好的,我已经在SO和网络上经历过许多类似的问题,但无济于事。 I'm just trying to overlay a transparent view on top of a SurfaceView.. and eventually draw a rectangle on it, such that it appears to overlay the camera preview. 我只是试图在SurfaceView顶部覆盖透明视图..并最终在其上绘制一个矩形,使其看起来覆盖相机预览。

I cannot get the custom view to be transparent, it blocks-out the underlying camera preview. 我不能让自定义视图透明,它会阻挡底层相机预览。 Any suggestions? 有什么建议么?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <FrameLayout 
        android:layout_alignParentTop="true"
        android:layout_width="fill_parent"
        android:layout_height="400dip">
        <SurfaceView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:id="@+id/drawSurface"  />
        <com.bobby.facecapture.FaceOverlayView
            android:id="@+id/drawOverlay"
            android:background="@android:color/transparent" 
            android:layout_width="200dip"     
            android:layout_height="200dip"    
        />
    </FrameLayout>
</RelativeLayout>

The 200x200 is just temporary, to prove that it is indeed overlaying the other view. 200x200只是暂时的,以证明它确实覆盖了另一个视图。

The FaceOverlayView is just a custom view - deriving directly from View, without overriding anything (for now). FaceOverlayView只是一个自定义视图 - 直接从View派生,而不会覆盖任何东西(现在)。

I get a black rectangle on top of the camera preview 我在相机预览的顶部有一个黑色矩形

Well, this resource file is the Barcode Scanner capture layout, which has a "viewfinder" layered over top of a camera preview. 好吧, 这个资源文件是条码扫描器捕获布局,它在摄像头预览的顶部有一个“取景器”。 They use their own @color/transparent resource, defined as <color name="transparent">#00000000</color> . 他们使用自己的@color/transparent资源,定义为<color name="transparent">#00000000</color> Perhaps poke around with their code and try to figure out where you and they differ. 也许用他们的代码来解决,并试图弄清楚你和他们的不同之处。

I have been doing exactly the same, have a customized view overlay on Camera preview. 我一直在做同样的事情,在相机预览上有自定义视图叠加。 Initially the view was opaque so the camera preview got blocked. 最初视图是不透明的,因此相机预览被阻止。 The problem was I was trying to draw an image to the canvas but specified the image as RGB_565, change it to ARGB_8888 works fine. 问题是我试图将图像绘制到画布但指定图像为RGB_565,将其更改为ARGB_8888工作正常。 You must have the alpha channel, and make sure the background of the custom view is set to transparent. 您必须具有Alpha通道,并确保将自定义视图的背景设置为透明。

I set the alpha channel to 0. It works like a charm. 我将alpha通道设置为0.它就像一个魅力。

I do it after I add the custom view runtime. 我添加自定义视图运行时后执行此操作。

(ImageView) myCustomView.setAlpha(0)

Have you tried setting both/either the src or the background to @null? 您是否尝试将src或背景设置为@null?

Also, if this avenue doesn't work out you could consider using a Path to draw onto the surface, which would allow you to dynamically change the size/location of the rectangle at runtime. 此外,如果此途径不起作用,您可以考虑使用路径绘制到曲面上,这将允许您在运行时动态更改矩形的大小/位置。

That would, however, require writing some code for a SurfaceView and handling the onDraw, which might be overkill for what you're doing. 但是,这需要为SurfaceView编写一些代码并处理onDraw,这可能对你正在做的事情有些过分。 Essentially it would mean making your custom class here an extension of SurfaceView, rather than something layered on top of one. 从本质上讲,这意味着在这里使您的自定义类成为SurfaceView的扩展,而不是在一个上面分层的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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