如何获得像Instagram一样具有1:1比例的Android Camera2? [英] How to get an Android Camera2 with 1:1 ratio like Instagram?

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

问题描述

我的问题很简单:

如何获得比例为1:1且没有Instagram之类变形的Android android.hardware.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项目

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆