首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Camera 2 Preview已拉伸

Camera 2 Preview已拉伸
EN

Stack Overflow用户
提问于 2019-05-14 14:14:54
回答 4查看 405关注 0票数 0

我目前正在制作一个使用Camera2应用程序接口的安卓应用程序。我正面临着AutoFitTextureView的问题,图像显示为拉伸。我的AutoFitTextureView代码如下所示

代码语言:javascript
复制
public class AutoFitTextureView extends TextureView {
private int mRatioWidth = 0;
private int mRatioHeight = 0;
private boolean mWithMargin = false;
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);
}

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

XML代码-

拉伸图像:

我使用的XML代码如下预览图所示:

代码语言:javascript
复制
 <com.qopius.AutoFitTextureViewNew
        android:id="@+id/texture"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         />
EN

回答 4

Stack Overflow用户

发布于 2019-05-14 14:29:39

试试这段代码

代码语言:javascript
复制
@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) {
            if (width > height * mRatioWidth / mRatioHeight) {
                setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
            } else {
                setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
}
}
票数 0
EN

Stack Overflow用户

发布于 2019-05-14 17:12:27

试试这个

代码语言:javascript
复制
@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);
        }
    }
}
票数 0
EN

Stack Overflow用户

发布于 2019-05-16 01:40:35

您需要将AutoFitTextureView放在允许其调整大小的父视图/容器中。如果父视图不允许这样做,那么无论你在它的onMeasure中做什么,它都会调整纹理视图的大小。

例如,Camera2Basic示例将AutoFitTextureView放在RelativeLayout中:

https://github.com/googlesamples/android-Camera2Basic/blob/master/Application/src/main/res/layout/fragment_camera2_basic.xml

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56124055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档