使用camera2设置全屏TextureView却不显示为全屏的解决办法
作者:互联网
找到自己TextureView的onMeasure()方法,如果使用的是官方demo,那代码应该如下:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (0 == mRatioWidth || 0 == mRatioHeight) {
setMeasuredDimension(width, height);
} else {
if (width < height * mRatioWidth / mRatioHeight) {
setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
} else {
setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
}
}
}
这段代码使得TextureView在宽高都不超过手机屏幕的情况下最大化显示。
解决方案是,让TextureView总是达到最大边界,超出部分不进行预览(实际上还是能拍到的)。代码如下:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (0 == mRatioWidth || 0 == mRatioHeight) {
setMeasuredDimension(width, height);
} else {
//注意这里骚操作,替换"小于号"为"大于号"
if (width > height * mRatioWidth / mRatioHeight) {
setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
} else {
setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
}
}
}
实际上就是把判断条件中的 “<” 替换成 “>” 即可,天知道这一小段代码害惨多少程序员。
上个效果图:
标签:mRatioWidth,width,int,camera2,mRatioHeight,height,TextureView,setMeasuredDimensi 来源: https://blog.csdn.net/baishuiniyaonulia/article/details/104114203