无法使用setLayoutParams调整相机预览的大小

时间:2018-09-09 03:02:19

标签: android camera surfaceview preview

我是Android的新手,正在开发相机应用程序。我使用Surfaceview来显示相机预览效果很好。然后使用setLayoutParams()调整无效的Surfaceview大小。 Surfaceview的大小未更改。

我的代码有什么问题吗?

非常感谢任何建议或话语。

非常感谢!

我的代码:

//init surfaceview and surfaceholder
private void initSurHolderRear()
{
    if ( null == mSurViewRear )
    {
        mSurViewRear = new SurfaceView(this);
    }

    WindowManager.LayoutParams parms;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
    {
        parms = new WindowManager.LayoutParams(
                1, 1,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT
        );
        parms.gravity = Gravity.START | Gravity.TOP;
    }
    else
    {
        parms = new WindowManager.LayoutParams(
                1, 1,
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT
        );
        parms.gravity = Gravity.START | Gravity.TOP;
    }

    ( (WindowManager) ( this.getSystemService(Context.WINDOW_SERVICE) ) ).addView(mSurViewRear, parms);
    mSurHolderRear = mSurViewRear.getHolder();
    mSurHolderRear.addCallback(this);
}


//start camera preview
private void startPreview() {

    try {
        mCamRear.setPreviewDisplay(mSurHolderRear);
        mCamRear.startPreview();

        startRearRecording(mSurHolderRear);

    } catch (Exception e) {
        e.printStackTrace();
        Log.v(LOG_TAG, "Failed to startPreview, e: " + e);
    }
}

//resize camera preview
public void resizePreview()
{
    ViewGroup.LayoutParams parms = mSurViewRear.getLayoutParams();
    parms.width = 1000;
    parms.height = 1000;

    mSurViewRear.setLayoutParams(parms);
}

0 个答案:

没有答案