我正在开发相机应用程序,我将使用前后相机。我正在使用这样的TextureView:
<TextureView
android:id="@+id/textureView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
/>
和代码背后:
renderer = new CameraRenderer(this, isFrontCamera);
textureView.setSurfaceTextureListener(renderer);
CameraRenderer
class implemet SurfaceTextureListener
public class CameraRenderer implements Runnable, TextureView.SurfaceTextureListener{...
我按照onSurfaceTextureAvailable
方法设置相机:
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
if (renderThread != null && renderThread.isAlive()) {
renderThread.interrupt();
}
renderThread = new Thread(this);
surfaceTexture = surface;
gwidth = width;
gheight = height;
// Open camera
int cameraId;
if(isFrontCamera)
cameraId = getFrontCamera();
else
cameraId = getBackCamera();
camera = Camera.open(cameraId);
renderThread.start();
}
问题:
当我想更改相机并单击按钮时,我正在创建一个新的CameraRenderer实例并设置为textureView
textureView.setSurfaceTextureListener(renderer);
但是在CameraRenderer类中的onSurfaceTextureDestroyed方法永远不会调用,也不会调用onSurfaceTextureAvailable方法。
我可以自己调用onSurfaceTextureDestroyed方法,但是我无法将第二个相机预览设置为textureView,因为onSurfaceTextureAvailable方法不会被第二次调用。
这是我的onSurfaceTextureDestroyed方法:
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
if (camera != null) {
camera.stopPreview();
camera.release();
}
if (renderThread != null && renderThread.isAlive()) {
renderThread.interrupt();
}
CameraFilter.release();
return true;
}
最后注意:我尝试将侦听器设置为null但不起作用。
那我怎样才能重新设置textureView Listener。提前谢谢!