Android 前摄像头预览不是全屏的

Android 前摄像头预览不是全屏的,android,camera,Android,Camera,我已经使用camera2 api在android上定制了一个摄像头。我目前面临一个间歇性的问题,一个设备,一个一加一。经检查,它与S3、S4、HTC(所有主要设备)、Moto(所有设备)等其他设备配合良好 请说明在OnePlus One和其他设备上解决此问题是否需要/需要特别的帮助 public class AutoFitTextureView extends TextureView { private int mRatioWidth = 0; private int mRatioHeight

我已经使用camera2 api在android上定制了一个摄像头。我目前面临一个间歇性的问题,一个设备,一个一加一。经检查,它与S3、S4、HTC(所有主要设备)、Moto(所有设备)等其他设备配合良好

请说明在OnePlus One和其他设备上解决此问题是否需要/需要特别的帮助

public class AutoFitTextureView extends TextureView {

private int mRatioWidth = 0;
private int mRatioHeight = 0;

public AutoFitTextureView(Context context) {
    this(context, null);
}

public AutoFitTextureView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

@SuppressLint("NewApi")
public AutoFitTextureView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

/**
 * Sets the aspect ratio for this view. The size of the view will be measured based on the ratio
 * calculated from the parameters. Note that the actual sizes of parameters don't matter, that
 * is, calling setAspectRatio(2, 3) and setAspectRatio(4, 6) make the same result.
 *
 * @param width  Relative horizontal size
 * @param height Relative vertical size
 */
@SuppressLint("NewApi")
public void setAspectRatio(int width, int height) {
    if (width < 0 || height < 0) {
        throw new IllegalArgumentException("Size cannot be negative.");
    }
    mRatioWidth = width;
    mRatioHeight = height;
    requestLayout();
}

@SuppressLint("NewApi")
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    if (mRatioWidth ==0 && mRatioHeight == 00) {
           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);
               }
           }
    }else {
        setMeasuredDimension(mRatioWidth, mRatioHeight);
    }
}
公共类AutoFitTextureView扩展了TextureView{
私有宽度=0;
私有内部高度=0;
公共AutoFitTextureView(上下文){
这个(上下文,空);
}
公共AutoFitTextureView(上下文、属性集属性){
这(上下文,属性,0);
}
@SuppressLint(“新API”)
公共AutoFitTextureView(上下文上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
}
/**
*设置此视图的纵横比。将根据纵横比测量视图的大小
*根据参数计算。请注意,参数的实际大小并不重要
*是,调用setAspectRatio(2,3)和setAspectRatio(4,6)会得到相同的结果。
*
*@param宽度相对水平尺寸
*@param高度相对垂直尺寸
*/
@SuppressLint(“新API”)
公共无效设置AspectRatio(内部宽度、内部高度){
如果(宽度<0 | |高度<0){
抛出新的IllegalArgumentException(“大小不能为负”);
}
宽度=宽度;
高度=高度;
requestLayout();
}
@SuppressLint(“新API”)
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
超级测量(宽度测量、高度测量);
如果(mRatioWidth==0&&mRatioHeight==00){
int width=MeasureSpec.getSize(widthmasurespec);
int height=MeasureSpec.getSize(heightMeasureSpec);
如果(0==mRatioWidth | | 0==mRatioHeight){
设置测量尺寸(宽度、高度);
}否则{
if(宽度<高度*mRatioWidth/mRatioHeight){
设置测量尺寸(宽度、宽度*mRatioHeight/mRatioWidth);
}否则{
设置测量尺寸(高度*mRatioWidth/mRatioHeight,高度);
}
}
}否则{
设置测量尺寸(mRatioWidth、mRatioHeight);
}
}
我们用link制作了这款相机


提前感谢

代码部分

if (mRatioWidth ==0 && mRatioHeight == 00) {
           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);
               }
           }
    }else {
        setMeasuredDimension(mRatioWidth, mRatioHeight);
    }

这是您想要的逻辑吗?

看起来代码部分

if (mRatioWidth ==0 && mRatioHeight == 00) {
           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);
               }
           }
    }else {
        setMeasuredDimension(mRatioWidth, mRatioHeight);
    }

这是您想要的逻辑吗?

您应该更改测量的宽度和高度以覆盖整个屏幕,而不是适合屏幕,如下所示

发件人:

if (width < height * mRatioWidth / mRatioHeight) 
这对我很管用。

您应该更改测量的宽度和高度以覆盖整个屏幕,而不是适合屏幕,如下所示

发件人:

if (width < height * mRatioWidth / mRatioHeight) 
这对我很管用。

嗨,Zephyr,谢谢您的回复。这个解决方案对我不起作用。主要问题是,只有一加一手机的前置摄像头预览不是全屏的。@Sirconrad,我想您的onMeasure()或onLayout()中可能遗漏了一些内容函数,因此请检查代码中的逻辑。@Sirconrad,对于One plus手机,可能需要安装其他示例应用程序,以检查该手机是否有一些特殊的技巧来实现全屏显示。或者使用该工具检查该手机的屏幕/显示信息。One plus手机使用Cynogen 11S基于Android的操作系统,所以这是一个定制的操作系统。@Zenhyr。我很确定这是一个主要问题,因为设置了摄像头预览方法。嗨,Zephyr,谢谢你的回复。这个解决方案对我不起作用。主要问题是只有一加一移动设备的前置摄像头预览不是全屏的。@Sirconrad,我只是想可能遗漏了什么在onMeasure()或onLayout()中插入函数,因此请检查代码中的逻辑。@Sirconrad,对于One plus手机,可能需要安装其他示例应用程序,以检查该手机是否有一些特殊的技巧来实现全屏显示。或者使用该工具检查该手机的屏幕/显示信息。One plus手机使用Cynogen 11S基于Android的操作系统,所以这是一个定制的操作系统。@Zenhyr。我很确定主要的问题在于设置相机预览方法。