【问题标题】:how to make my flashlight not o open instantly when i open the program?如何让我的手电筒在打开程序时不会立即打开?
【发布时间】:2013-12-04 18:13:31
【问题描述】:
    // Get the camera
        private void getCamera() {
            if (camera == null) {
                try {
                    camera = Camera.open();
                    params = camera.getParameters();
                } catch (RuntimeException e) {
                    Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
                }
            }
        }





// Turning On flash
    private void turnOnFlash() {
        if (!isFlashOn) {
            if (camera == null || params == null) {
                return;
            }
        // play sound
        playSound();

        params = camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(params);
        camera.startPreview();
        isFlashOn = true;

        // changing button/switch image
        toggleButtonImage();
    }

}

上面是我与 flash on 相关的代码,任何建议,如何让 flash 在我的应用打开时不打开,但只有在我按下按钮时才打开

【问题讨论】:

  • 那么在您的代码中,turnFlashOn() 是从哪里调用的?
  • 你不能帮我吗? ://
  • 不,因为您没有在任何调用turnFlashOn() 的地方包含代码。如果你有,我就不会问电话是从哪里来的。 :-) 搜索您的代码,并找出调用 turnFlashOn() 的位置(不是定义,而是调用)。

标签: flashlight


【解决方案1】:

我猜你在onCreate() 中调用turnFlashOn() 并且没有ClickListener 按钮。但是您可以在onStart()onResume() 中调用turnFlashOn()

要通过按下按钮打开它,您应该执行类似的操作

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_view);

    flashlight_button = (Button) findViewById(R.id.your_button);
    flashlight_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            getCamera();
            turnOnFlash();
        }
    });

}

我为 Android 构建了一个开源手电筒,您可以通过 Flashlight by Joe github 查看它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多