我想添加一個按鈕(我已經包括在我的activity_main中)到我的應用程序,它將在按下時啓動頻閃效果,並在再次按下時停止。我不關心速度,只是它重複切換flash_mode_torch和flash_mode_off,直到再次按下按鈕。如何切換手電筒模式?
我曾嘗試:
- 通過處理器
- 創建一個單獨的類
包含處理程序,因爲有主活動無意推出沒有工作的單獨的類或者在清單中,因爲它的代碼沒有完成,因爲我想問一下它是如何以最簡單的方式完成的。
我想添加一個按鈕(我已經包括在我的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);
}
}
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複製粘貼其他人的代碼,至少有禮貌給他們信用 – 2015-03-13 10:15:19
至少我在StackOverFlow上提供代碼。鏈接可能會被破壞。把它看成是積極的 – Bhaskar 2015-03-13 10:17:22
這不是關於斷開的鏈接或代碼可用的地方,而是關於爲其他人的工作獲得功勞 – 2015-03-13 10:20:31
而如何或爲何沒有工作? – 2015-03-13 09:37:30
我的更新說明陳述了原因。這應該不是那麼複雜,但Google或Stackoverflow上沒有明確定義選通方法的答案。 – 2015-03-13 10:03:11
https://github.com/stwalkerster/strobelight可能會對您有所幫助 – 2015-03-13 10:06:46