如何切换手电筒模式



我想在我的应用程序中添加一个按钮(我已经包含在我的activity_main中),该按钮将在按下时启动频闪效果,并在再次按下时停止。我不在乎速度,只是它反复切换flash_mode_torch和flash_mode_off,直到再次按下按钮。

我试过:

  • 使用处理程序
  • 创建单独的类

包含处理程序的单独类不起作用,因为主活动或清单中没有启动意图,因为它的代码尚未完成,因为我想在这里问它是如何以最简单的方式完成的。

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);
        }
    }

频闪跑者.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);
    }
}

主.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>

最新更新