具有正确宽高比的android textureview全屏预览 [英] android textureview full screen preview with correct aspect ratio
问题描述
我一直在使用来自Google的camera2 api演示,不幸的是该示例应用程序已构建环顾四周后,我可以确定这是由AutoFitTextureView覆盖了onMeasure()
方法造成的,如下图所示:
I have been working with the camera2 api demo from google and unfortunately the sample application is built to display the textureview preview at approximately 70% of the screen height, after looking around I was able to determine that this was being caused by the AutoFitTextureView overriding the onMeasure()
method as shown below:
@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);
}
}
}
我尝试通过在setMeasuredDimension(width, height);
中设置正确的高度和宽度来更正此问题,这解决了高度问题,并从Textureview提供了全屏预览,但是纵横比在每个设备上都完全被破坏和扭曲了,这是什么?解决此问题的标准方法是什么?我发现Play商店中的许多应用都找到了解决此问题的方法,但还没有找到解决办法,任何帮助都将走很长一段路要走.
I attempted to correct this by setting the correct heights and widths in setMeasuredDimension(width, height);
, this fixed the height issue and gave me a full screen preview from the textureview, however the aspect ratio is completely broken and warped on every device, what is the standard way of fixing this? I see many apps on the play store have found a way to solve this problem but havent been able to track down a fix, any help will go a long way, thanks.
推荐答案
我能够通过切换setMeasuredDimension();
I was able to fix the issue by switching setMeasuredDimension();
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(height * mRatioWidth / mRatioHeight, height);
} else {
setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
}
}
这篇关于具有正确宽高比的android textureview全屏预览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!