首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TextureView setBackgroundColor不工作

TextureView setBackgroundColor不工作
EN

Stack Overflow用户
提问于 2013-09-22 13:33:43
回答 1查看 6.2K关注 0票数 7

我正在尝试简单地更改TextureView的背景颜色。我希望通过半透明的相机预览可以看到这种颜色。如果我从onSurfaceTextureAvailable中完全删除了摄像头预览,视图仍然是白色的(或者看不到)。有什么想法吗?我正在尝试实现一个TextureView盒子,有时包含相机预览,另一些时候是我选择的纯颜色。谢谢。

代码语言:javascript
运行
复制
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

        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2016-10-25 05:15:52

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

代码语言:javascript
运行
复制
textureView.setOpaque(false);

但是,当你这样做的时候,TextureView的背景颜色将变得无关紧要。所以-我把我的TextureView放在一个FrameLayout里面并设置它的背景颜色。当你这样做的时候,FrameLayout的背景颜色就是你看到的“背后”的颜色,或者如果TextureView是空的。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18940694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档