本文介绍了如何获得像Instagram一样具有1:1比例的Android Camera2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单:

我使用GoogeSamples项目 android-Camera2Basic 进行了测试.但是,当我以1:1的比例更改预览时,图像变形了.有人对此有想法吗?

I tested with the GoogeSamples project android-Camera2Basic. But when I change the preview with a ratio of 1:1 image is deformed. Does anyone have an idea on this?

推荐答案

感谢@CommonsWare.

Thanks @CommonsWare.

我按照您的建议使用了负边距(顶部和底部),并且有效.

I followed your advice using negative margin (top and bottom) and it works.

为此,我只需更新 AutoFitTextureView GoogeSamples项目 android-Camera2Basic 这样:

To do that, I just update AutoFitTextureView the GoogeSamples project android-Camera2Basic this way:

public class AutoFitTextureView extends TextureView {

    //...
    private boolean mWithMargin = false;

    //...

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int margin = (height - width) / 2;

        if(!mWithMargin) {
            mWithMargin = true;
            ViewGroup.MarginLayoutParams margins = ViewGroup.MarginLayoutParams.class.cast(getLayoutParams());
            margins.topMargin = -margin;
            margins.bottomMargin = -margin;
            margins.leftMargin = 0;
            margins.rightMargin = 0;
            setLayoutParams(margins);
        }

        if (0 == mRatioWidth || 0 == mRatioHeight) {
            setMeasuredDimension(width, height);
        } else {
            if (width < height) {
                setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
            } else {
                setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
            }
        }
    }
}

这篇关于如何获得像Instagram一样具有1:1比例的Android Camera2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 17:47