如何调用onSurfaceTexture可以从onCreate中获取?

时间:2018-06-05 19:12:20

标签: android android-fragments android-textureview

我的MainActivity中有两个片段。我的应用程序加载第一个片段,然后单击一个按钮,转到包含textureView的第二个片段以显示相机。当我初始化并在onCreate()中调用textureView时,一切都很好。 但是当用户处于第一个片段(很长一段时间)时,我不想要初始的空闲textureView。

所以需要在下面的另一个方法中调用它,但会导致不可见的textureView。 android:hardwareAccelerated="true"不起作用。 如何调用onSurfaceTexture可以从onCreate中获取?

MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState)  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

           texture();
        }
   });

}  //////// CLOSE OF onCreate !


public void texture(){
    mTextureView =(TextureView)findViewById(R.id.tview);
    mTextureView.setSurfaceTextureListener(this);
}






@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
    mCamera = Camera.open();


    mTextureView.setLayoutParams(new FrameLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));

    try {
        mCamera.setPreviewTexture(surface);
    } catch (IOException t) {
    }

    mCamera.startPreview();

}

@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
    // Ignored, the Camera does all the work for us
}

@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
    mCamera.stopPreview();
    mCamera.release();
    return true;
}

@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
    // Update your view here!
}

0 个答案:

没有答案