Java 安卓全屏摄像机

Java 安卓全屏摄像机,java,android,camera,Java,Android,Camera,我想在摄影机片段上将纹理视图设置为全屏。现在底部部分显示为白色块 public class AutoFitTextureView extends TextureView { private int mRatioWidth = 0; private int mRatioHeight = 0; public AutoFitTextureView(Context context) { this(context, null); } publi

我想在摄影机片段上将纹理视图设置为全屏。现在底部部分显示为白色块

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

    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
     */
    public void setAspectRatio(int width, int height) {
        if (width < 0 || height < 0) {
            throw new IllegalArgumentException("Size cannot be negative.");
        }
        mRatioWidth = width;
        mRatioHeight = height;
        requestLayout();
    }

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

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


将layout高度和布局宽度设置为与父级匹配,而不是将内容换行

我也会去掉你的按钮所在的框架布局,只把你的按钮放在屏幕的底部,除非你用它做其他的事情

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.AutoFitTextureView
        android:id="@+id/texture"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <Button
        android:id="@+id/picture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="@string/picture"/>

</RelativeLayout>

将layout高度和布局宽度设置为与父级匹配,而不是将内容换行

我也会去掉你的按钮所在的框架布局,只把你的按钮放在屏幕的底部,除非你用它做其他的事情

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.AutoFitTextureView
        android:id="@+id/texture"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <Button
        android:id="@+id/picture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="@string/picture"/>

</RelativeLayout>


但它仍然不起作用。现在
picture
按钮在左下角。@RonakPatel现在应该可以工作了。改为将其编辑为填充父项。无差异。我仍然可以看到空白,但它仍然不起作用。现在
picture
按钮在左下角。@RonakPatel现在应该可以工作了。改为将其编辑为填充父项。无差异。我仍然可以看到空白。