android view background color,android – TextureView setBackgroundColor无法正常工作

我试图简单地改变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

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值