如果我将CameraPreview可见性设置为GONE,是否可以节省电量?

时间:2016-08-21 03:50:20

标签: android android-camera batterylevel

我有一个名为CameraPreview的简单类,这是代码:

    public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;

    public CameraPreview(Context context) {
        super(context);
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    }

    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mCamera = Camera.open();
        } catch (RuntimeException e) {}

        try {
            SurfaceHolder someHolder = holder;
            mCamera.setPreviewDisplay(someHolder);
            mCamera.startPreview();
        } catch (IOException e) {
        }

    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        if (mHolder.getSurface() == null)
            return;

        try {
            mCamera.stopPreview();
        } catch (Exception e){}

        mCamera.setDisplayOrientation(90);
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e){}
    }
}

很明显,当应用程序在屏幕上显示CameraPreview时,它会更快耗电,我的问题是,如果我将此视图设置为View.GONE,电池会发生什么?< / p>

1 个答案:

答案 0 :(得分:0)

根据this SO answer设置,SurfaceView可见性为GONE将调用surfaceDestroyed方法。

因此,由于您正在停止相机,因此您不会消耗用户的电池。