TextureView的setBackgroundColor不起作用

7

我想简单地更改TextureView的背景颜色。我希望这个颜色可以透过半透明的相机预览看到。但出于某种原因,我无法实现这一点。如果我从onSurfaceTextureAvailable中完全删除相机预览,那么视图将保持白色(或者无法看到)。你有什么想法吗?我想要实现一个纹理视图框,有时包含相机预览,有时是我选择的纯色。谢谢。

import android.graphics.Color;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.Surface;
import android.view.TextureView;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import java.io.IOException;

public class MainActivity extends Activity {

    private TextureView mTextureView;
    FrameLayout screenFrame;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        screenFrame = (FrameLayout)findViewById(R.id.screenFrame);

        setVideoScreens();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private void setVideoScreens()
    {
        final LiveCameraActivity livecam = new LiveCameraActivity();

        mTextureView = new TextureView(this);
        mTextureView.setSurfaceTextureListener(livecam);

        FrameLayout mainFrame = (FrameLayout)findViewById(R.id.mainFrame);
        mainFrame.addView(mTextureView);

        int width = 640;
        int height = 480;

        mTextureView.setLayoutParams(new FrameLayout.LayoutParams(
                width, height));
        mTextureView.setY(200);
        mTextureView.setX(200);
        mTextureView.setBackgroundColor(Color.BLUE);
        mTextureView.setAlpha(.5f);
    }

    public class LiveCameraActivity extends Activity implements TextureView.SurfaceTextureListener
    {
        private Camera mCamera;

        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
        }


        public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
        {
            mCamera = Camera.open();

            try
            {
                mCamera.setPreviewTexture(surface);
                mCamera.startPreview();
            }
            catch (IOException ioe)
            {
                // Something bad happened
            }
        }

        public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height)
        {
            // Ignored, Camera does all the work for us
        }

        public boolean onSurfaceTextureDestroyed(SurfaceTexture surface)
        {
            mCamera.stopPreview();
            mCamera.release();
            return true;
        }

        public void onSurfaceTextureUpdated(SurfaceTexture surface)
        {
            // Invoked every time there's a new Camera preview frame

        }
    }
}

请分享完整的代码,这样我们才能正确地帮助您。 - Salman Khakwani
同样的问题。有解决方案吗? - Alexis
1个回答

7

我的解决方案是将opaque设置为false:

textureView.setOpaque(false);

但是,如果你这样做,TextureView的背景颜色将不再重要。因此,我将我的TextureView放在一个FrameLayout中,并设置了它的背景颜色。这样做后,FrameLayout的背景颜色就是你看到的“背景”,或者说是当TextureView为空时的颜色。

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