如何在关闭应用程序之前停止textureview侦听器并显示图像

时间:2014-04-10 07:29:47

标签: java android textureview

我有一个简单的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中显示来自SD卡的文件加载。

这个伪代码可以是这样的:

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布局的情况下执行此操作吗? 提前谢谢!

2 个答案:

答案 0 :(得分:2)

你手动NSManagedObject Class Reference,我很害怕。最好的办法是在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);
}

答案 1 :(得分:0)

无需创建其他图片。只需使用

textureView.getBitmap(file.getAbsolutePath);