【问题标题】:TextureView setBackgroundColor not workingTextureView setBackgroundColor 不起作用
【发布时间】:2013-09-22 05:33:43
【问题描述】:

我正在尝试简单地更改 TextureView 的背景颜色。我希望通过半透明相机预览可以看到这种颜色。由于某种原因,它没有。如果我从 onSurfaceTextureAvailable 中完全删除相机预览,则视图保持白色(或看不到)。有任何想法吗?我正在尝试实现一个 TextureView 框,它有时包含相机预览,有时是我选择的纯色。谢谢。

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

        }
    }
}

【问题讨论】:

  • 请分享完整的代码,以便我们提供适当的帮助。
  • 同样的问题。有什么解决办法吗?

标签: android background textureview


【解决方案1】:

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

textureView.setOpaque(false);

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

【讨论】:

    猜你喜欢
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    相关资源
    最近更新 更多