我有一个简单的 xml 布局,如下所示,FrameLayout 中只有一个 TextureView。
以及以下主要活动:
public class MainActivity extends Activity
implements TextureView.SurfaceTextureListener {
private static final String TAG = "MainActivity";
// Local references to layout components
private TextureView mTxvCameraPreview;
// Camera manager
private CameraManager mCameraManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initialize layout and components
setContentView(R.layout.activity_main);
mTxvCameraPreview = (TextureView)findViewById(R.id.txv_camera_preview);
// Camera managing
mCameraManager = new CameraManager(this);
mTxvCameraPreview.setSurfaceTextureListener(this);
}
@Override
protected void onResume() {
super.onResume();
if (mTxvCameraPreview.isAvailable())
mCameraManager.startPreview(mTxvCameraPreview.getSurfaceTexture());
}
@Override
protected void onPause() {
super.onPause();
mCameraManager.stopPreview();
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
mCameraManager.startPreview(surface);
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
mCameraManager.stopPreview();
return true;
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {} // Unused
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {} // Unused
}
对于每一帧,我都会做一些详细说明,没关系。我想要的是添加一个按钮来停止相机并在 TextureView 中显示从 sdcard 加载的文件。
它的伪代码可以是这样的:
public void onButtonClicked(View view) {
// stop the surface listener (it is needed?)
File imgFile = new File(“/sdcard/Images/elaboration_result.jpg”);
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
// show myBitmap on TextureView (if possible)
}
可以在不修改 xml 布局的情况下做到这一点吗?提前致谢!