相机预览中setAlpha无效

4

我想在Android系统上创建相机预览。我还想让它透明度为50%。这里有个问题。setAlpha在我使用时无法正常工作。

这是我的服务,我使用它将SurfaceView添加到窗口中:

WindowManager.LayoutParams tmp = new WindowManager.LayoutParams(
            measuredWidth,measuredHeight,
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                    | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
            PixelFormat.TRANSPARENT);
    /*s.setZOrderOnTop(true);
    s.setAlpha(0.5f);
    tmp.alpha=0.0f;

    wm.addView(s, tmp);*/

    LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View mCameraView = layoutInflater.inflate(R.layout.camera_surface, null);
    StunGunPreview preview=(StunGunPreview) mCameraView.findViewById(R.id.textureView);
    wm.addView(mCameraView, tmp);



public class StunGunPreview extends SurfaceView implements
        SurfaceHolder.Callback {
    SurfaceHolder holder;
    static Camera mCamera;
    Context c;
    WindowManager wm;

    public StunGunPreview(Context context, AttributeSet attrs) {
        super(context,attrs);
        c = context;
        wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

        holder = this.getHolder();
        holder.addCallback(this);
        holder.setFormat(PixelFormat.TRANSPARENT);
       setZOrderOnTop(true);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        start_camera();
    }

    private void start_camera() {
        try {
            mCamera = Camera.open();
        } catch (RuntimeException e) {
            return;
        }
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (Exception e) {
            return;
        }
    }
}
1个回答

4
一个SurfaceView由两部分组成,即Surface和View。调用View的setAlpha()方法只会影响在View上绘制的内容,这对你没什么帮助,因为Camera帧被发送到了Surface。
Surface是一个独立的层。当前API没有提供设置“平面透明度”的方法,因此您只能得到像素值所具有的透明度。对于相机和视频输出,YUV到RGB转换总是生成完全不透明的像素。
要实现您想要的效果,需要将相机预览发送到SurfaceTexture,这会将帧转换为OpenGL ES的“外部”纹理。然后,您可以将其呈现在部分透明的四边形上。这需要一些工作量,但是一旦您做到了,就可以对实时摄像头图像做任何事情(例如将其包裹在球体周围或将其弹跳到屏幕上)。
Grafika中的“从相机获取纹理”的Activity中可以找到一个简单的示例。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接