【问题标题】:How to toggle flashlight mode?如何切换手电筒模式?
【发布时间】:2015-03-13 09:30:47
【问题描述】:

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

我试过了:

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

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

【问题讨论】:

  • 它是如何或为什么不起作用的?
  • 我更新的描述说明了原因。这不应该那么复杂,但在 Google 或 Stackoverflow 上还没有明确定义 strobe 方法的答案。
  • github.com/stwalkerster/strobelight 可能会有所帮助
  • 如果我们没有看到你做了什么,很难解释为什么它不起作用。
  • @TimCastelijns 谢谢,这就是我想要的!

标签: java android flashlight


【解决方案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>

【讨论】:

  • 如果你要从 github 复制粘贴别人的代码,至少有礼貌地给予他们信任
  • 至少我在 StackOverFlow 上提供代码。链接可能会损坏。采取积极的态度
  • 这不是关于断开的链接或代码可用的地方,而是关于为别人的工作赢得荣誉
  • @Bhaskar 闪光灯活动仅在从清单应用程序活动中删除主要活动(手电筒)时启动。如何同时启动两者?
  • 此代码不适用于所有类型的手机。我们如何使用表面视图来实现。
猜你喜欢
  • 2022-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-15
  • 2017-04-11
  • 2014-06-17
  • 2021-08-28
相关资源
最近更新 更多