Amazing Tutorials for Android Developers...

Friday, May 17, 2013

Strobe Light Demo

This demo example used to Camera Flash light effect(StrobeLight).


StrobeLightConfig.java

package com.stwalkerster.android.apps.strobelight;

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

package com.stwalkerster.android.apps.strobelight;

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>




click here to Download.

5 comments:

  1. Can you please repair the link or provide a new one in order to be able to download the example?
    Many thanks, keep with the good job!

    my email is : iya316@hotmail.com

    ReplyDelete
  2. strobe light do't work on motorola and samsang works fine on htc, my quistion is how to implement surfaceview or any alternative to storb light which work fine on all devices

    ReplyDelete
  3. Please reup project files

    ReplyDelete
  4. Not working in my Nexus. Is it device dependent?

    ReplyDelete

Animation Tutorial

Animation Image Used in Android Tutorial GIF Image used in Android Application. for that you need to use following code implementation...