Use camera2 set up full-screen TextureView not appear as full-screen solution

Find their TextureView of onMeasure () method, if you are using the official demo, that the code should be as follows:

@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);
        }
    }
}

This code makes TextureView maximized in the case of width and height does not exceed the phone screen.
The solution is to let TextureView always reach the maximum boundary, not excess preview (in fact, still be able to shoot). code show as 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);
        }
    }
}

In fact, the determination condition of "<" replaced by ">" to, you know how much a piece of code that a programmer victimized. .

Published 208 original articles · won praise 844 · Views 1.22 million +

Guess you like

Origin blog.csdn.net/baishuiniyaonulia/article/details/104114203