2015-03-13 1 views
0

Je veux ajouter un bouton (que j'ai inclus dans mon activity_main) à mon application qui lancera un effet stroboscopique quand on le pressera et s'arrêtera lorsqu'on le pressera à nouveau. Je ne m'inquiète pas de la vitesse, juste que ça change plusieurs fois flash_mode_torch et flash_mode_off jusqu'à ce que le bouton soit de nouveau pressé.Comment basculer en mode lampe de poche?

J'ai essayé:

  • l'aide d'un gestionnaire
  • Création d'une classe séparée

La classe séparée contenant le gestionnaire n'a pas fonctionné parce qu'il n'y avait pas d'intention de l'activité principale de lancer ou dans le manifeste parce que le code pour cela n'est pas fini parce que je veux demander ici comment cela se fait de la manière la plus simple possible.

+0

Et comment ou pourquoi at-il pas fonctionné? –

+0

Ma description mise à jour indique pourquoi. Cela ne devrait pas être si compliqué mais il n'y a pas de réponse sur Google ou Stackoverflow qui définit clairement une méthode stroboscopique. –

+2

https://github.com/stwalkerster/strobelight peut être utile –

Répondre

-1

StrobeLightConfig.java

import android.app.Activity; 
    import android.content.Intent; 
    import android.hardware.Camera; 
    import android.os.Bundle; 
    import android.os.Handler; 
    import android.view.View; 
    import android.widget.ImageButton; 
    import android.widget.SeekBar; 
    import android.widget.SeekBar.OnSeekBarChangeListener; 

    public class StrobeLightConfig extends Activity { 

     boolean check = false; 

     Camera sandy; 
     StrobeRunner runner; 
     Thread bw; 
     ImageButton btnClick; 

     public final Handler mHandler = new Handler(); 

     public final Runnable mShowToastRunnable = new Runnable() { 
      public void run() { 

      } 
     }; 

     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 

      btnClick = (ImageButton) findViewById(R.id.btnSwitch); 


      runner = StrobeRunner.getInstance(); 
      runner.controller = this; 

      if (runner.isRunning) { 

      } else { 
       try { 
        sandy = Camera.open(); 

        if (sandy == null) { 
         return; 
        } 

        sandy.release(); 
       } catch (RuntimeException ex) { 
        return; 
       } 
      } 

      bw = new Thread(runner); 
      bw.start(); 

      btnClick.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 
        // TODO Auto-generated method stub 
        if (check) { 
         bw = new Thread(runner); 
         bw.start(); 
         check = false; 
        } else { 
         check = true; 
         runner.requestStop = true; 
        }    
       } 
      }); 

      final SeekBar skbar = (SeekBar) findViewById(R.id.SeekBar01); 
      skbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 

       @Override 
       public void onStopTrackingTouch(SeekBar seekBar) { 
        // TODO Auto-generated method stub 

       } 

       @Override 
       public void onStartTrackingTouch(SeekBar seekBar) { 
        // TODO Auto-generated method stub 

       } 

       @Override 
       public void onProgressChanged(SeekBar seekBar, int progress, 
         boolean fromUser) { 


        runner.delay = 101 - progress; 
        runner.delayoff = 101 - progress; 

       } 
      }); 

     } 

     @Override 
     protected void onStop() { 
    //  runner.requestStop = true; 

      super.onStop(); 
     } 

     @Override 
     public void onBackPressed() { 
      // TODO Auto-generated method stub 
    //  super.onBackPressed(); 

      Intent startMain = new Intent(Intent.ACTION_MAIN); 
      startMain.addCategory(Intent.CATEGORY_HOME); 
      startActivity(startMain); 

     } 

    } 

StrobeRunner.java

import android.hardware.Camera; 

public class StrobeRunner implements Runnable { 

    protected StrobeRunner() 
    { 

    } 

    public static StrobeRunner getInstance() 
    { 
     return (instance == null ? instance = new StrobeRunner() : instance); 
    } 

    private static StrobeRunner instance; 


    public volatile boolean requestStop = false; 
    public volatile boolean isRunning = false; 
    public volatile int delay = 10; 
    public volatile int delayoff = 500; 
    public volatile StrobeLightConfig controller; 
    public volatile String errorMessage = ""; 

    @Override 
    public void run() { 
     if(isRunning) 
      return; 

     requestStop=false; 
     isRunning = true; 

     Camera cam = Camera.open(); 

     Camera.Parameters pon = cam.getParameters(), poff = cam.getParameters(); 

     pon.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); 
     poff.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); 

     while(!requestStop) 
     { 
      try{ 
       cam.setParameters(pon); 
       Thread.sleep(delay); 
       cam.setParameters(poff); 
       Thread.sleep(delayoff); 
      } 
      catch(InterruptedException ex) 
      { 

      } 
      catch(RuntimeException ex) 
      { 
       requestStop = true; 
       errorMessage = "Error setting camera flash status. Your device may be unsupported."; 
      } 
     } 

     cam.release(); 

     isRunning = false; 
     requestStop=false; 

     controller.mHandler.post(controller.mShowToastRunnable); 
    } 

} 

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/TableLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 

    <SeekBar 
     android:id="@+id/SeekBar01" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:max="100" 
     android:paddingLeft="5dp" 
     android:paddingRight="5dp" 
     android:progress="0" 
     android:layout_alignParentTop="true" 
     > 
    </SeekBar> 

    <ImageButton 
     android:id="@+id/btnSwitch" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/w_led_on" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="50dp"/> 

</RelativeLayout> 
+0

Si vous allez copier coller le code de quelqu'un d'autre de github, au moins avoir la courtoisie de leur donner crédit –

+0

au moins je fais le code disponible sur StackOverFlow .. Liens pourraient se briser. Prenez-le positif – Bhaskar

+0

Il ne s'agit pas de liens brisés ou de code disponible, mais de crédit pour le travail de quelqu'un d'autre –