我有一个简单的 xml 布局,如下所示,只有一个 FrameLayout 内的 TextureView。
以及以下主要 Activity :
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 布局的情况下做到这一点? 预先感谢!
最佳答案
你cannot set the content of a TextureView恐怕是手动的。你最好的选择是在你的TextureView之上创建一个新的ImageView,并在需要时设置它的图像。
public void onButtonClicked(View view) {
// if we don't need to keep previewing new frames, stop the preview
mCameraManager.stopPreview();
// now, show our ImageView (which should be in front of the TextureView)
Bitmap myBitmap = BitmapFactory.decodeFile(file_path_here);
myImageView.setImageBitmap(myBitmap);
myImageView.setVisibility(View.VISIBLE);
}
关于java - 如何在关闭应用程序之前停止纹理 View 监听器并显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22981448/