簡體   English   中英

頻閃燈工作正常,但單擊時效果不佳

[英]Strobe light is working fine but doesn't work very well on click

我創建了以下頻閃燈。 我希望它在按下按鈕時開始,如果再次按下則停止。 現在,當它打開並單擊時,它會關閉,但是再次單擊時,什么也不會發生!

沒有點擊偵聽器,它會在應用程序啟動時啟動,但效果很好,但是無法停止它。

    public class Small extends Activity {

    private MMAdView adViewFromXml;
    RefreshHandler handler;
    ImageButton knob;
    int n=100000;
    Camera mCamera;
    Parameters params;
    int delay = 400; // in ms
    public boolean on;
    public boolean works;
    Thread logotimer;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_small_button);

        knob = (ImageButton) findViewById(R.id.pic);
        strobe();

        knob.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View iv) {
                  if(works == true){
                      logotimer.interrupt();
                  }else if(works != true)
                  {
                      strobe();
                  }
              }
            });

                }); 

    }

    /** Turn the devices FlashLight on */
    public void turnOn() {
      if (mCamera != null) {
        // Turn on LED
        params = mCamera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_TORCH);
        mCamera.setParameters(params);
        mCamera.startPreview();
        on = true;
      }

    }

    /** Turn the devices FlashLight off */
    public void turnOff() {
      // Turn off flashlight
      if (mCamera != null) {
        params = mCamera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_OFF);
        mCamera.setParameters(params);
        mCamera.stopPreview();


      }
      on = false;
    }

    /** Toggle the flashlight on/off status */
    /*public void toggleFlashLight() {
        if (!on) { // Off, turn it on
        turnOn();
      } else { // On, turn it off
        turnOff();
      }
}*/
    private void strobe(){
      Thread logotimer = new Thread() {
        public void run() {
          try {
            // Switch on the cam for app's life
            if (mCamera == null) {
              // Turn on Cam
              try{
                mCamera = Camera.open();
              } catch (RuntimeException e) {
                Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
              }
              try {
                mCamera.setPreviewDisplay(null);
              } catch (IOException e) {
                e.printStackTrace();
              }
              mCamera.startPreview();
            }
            int logotimer = 0;
            while(!interrupted() && logotimer <5000) {
                logotimer = logotimer ++;
                works = true;

                if (!on) { // Off, turn it on
                    turnOn();
                  } else if(on == true) { // On, turn it off
                    turnOff();
                  }
                sleep(delay);
            }
            if (mCamera == null) {
              mCamera.stopPreview();
              mCamera.release();
              mCamera = null;
            }
          } catch (InterruptedException e){ 

            e.printStackTrace(); 
          }
        }
      };logotimer.start();
    }
}

日志貓:

01-08 15:17:33.807: W/System.err(28814): java.lang.InterruptedException
01-08 15:17:33.808: W/System.err(28814):    at java.lang.VMThread.sleep(Native Method)
01-08 15:17:33.808: W/System.err(28814):    at java.lang.Thread.sleep(Thread.java:1013)
01-08 15:17:33.808: W/System.err(28814):    at java.lang.Thread.sleep(Thread.java:995)
01-08 15:17:33.809: W/System.err(28814):    at com.light.oid.Small$4.run(Small.java:163)

在onClick內添加strobe()調用:

@Override
public void onClick(View iv) {
    if(works) {
        logotimer.interrupt();

    } else {
        //mCamera = Camera.open(); //remove this
        //and add strobe()
        strobe(); 
    }
}

另外,您具有全局Thread logotimer,但是在strobe()方法中,您將創建一個具有相同名稱的局部變量,因此,在單擊按鈕時,logotimer為null,因為局部陰影覆蓋了全局變量。

在strobe()中,更改:

 private void strobe(){
     Thread logotimer = new Thread() {
         public void run() {

至:

 private void strobe(){
     logotimer = new Thread() {
         public void run() {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM