Camera 2 Preview растягивается - PullRequest
0 голосов
/ 14 мая 2019

В настоящее время я делаю приложение для Android, используя Camera2 API. Я столкнулся с проблемой с AutoFitTextureView, изображение растянуто. Мой код для AutoFitTextureView как показано ниже

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 Code-

Растянутое изображение:

enter image description here

Код XML, который я использую, выглядит так, как показано ниже: предварительный просмотр растягивается -

 <com.qopius.AutoFitTextureViewNew
        android:id="@+id/texture"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         />

Ответы [ 3 ]

0 голосов
/ 14 мая 2019

Попробуйте это

@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 голосов
/ 15 мая 2019

Вам необходимо разместить AutoFitTextureView в родительском представлении / контейнере, которое позволяет ему регулировать свой размер. Если родительский View не разрешит этого, он изменит размер текстуры независимо от того, что вы делаете в его onMeasure.

Например, образец Camera2Basic помещает AutoFitTextureView в RelativeLayout:

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

0 голосов
/ 14 мая 2019

Попробуйте этот код

@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);
}
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...