简体   繁体   English

SeekBar值未在while循环中实现

[英]SeekBar value isn't actualized in while loop

I am trying to create a flashlight app with a strobe (blink) effect which I can change with a SeekBar (to change the frequency of the strobe).My goal is that I would be able to change the frequency while it's ON (blinking) and if the Seekbar value goes to 0 the Flash should be ON(without strobe/blinking) . 我正在尝试创建一个具有频闪 (闪烁)效果的手电筒应用程序,可以使用SeekBar进行更改(以更改频闪的频率)。我的目标是在打开(闪烁)时可以更改频率如果Seekbar值变为0,则闪光灯应处于ON状态(无频闪/闪烁) My problem is that I can turn it ON (with a Switch Btn, but if I increase the frequency once I am not able to turn OFF the strobe (but the Flash is still ON without blinking) with the Seekbar . If I put the Seekbar value to 0 it's still blinking with the lowest frequency. 我的问题是,我可以把它打开(带开关BTN,但如果我增加频率,一旦我不能关掉闪光灯 (但闪光灯仍然打开无闪烁) 与搜索条 。如果我把搜索条值设为0时,它仍以最低频率闪烁。

I would be so glad if you could help me. 如果您能帮助我,我会很高兴。

Code: public class MainActivity extends AppCompatActivity { 代码:公共类MainActivity扩展了AppCompatActivity {

Camera cam;
Parameters camParams;
boolean hasCam;
int freq;
StroboRunner sr;
Thread t;
SeekBar seekBar;

Switch mySwitch;
SeekBar skBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mySwitch = (Switch) findViewById(R.id.mySwitch);
    // set the Switch to off
    mySwitch.setChecked(false);
    //now you need a listener to check changes in state
    mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            turnOnOff(isChecked);
        }
    });
    skBar = (SeekBar) findViewById(R.id.seekBar);
    skBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            freq = progress;
        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
    });
}
@Override
protected void onResume() {
    super.onResume();
    try {
        cam = Camera.open();
        camParams = cam.getParameters();
        cam.startPreview();
        hasCam = true;
    }catch(Exception e){
    }
}
private void turnOnOff(boolean on){
    if(on) {
        if(freq != 0){
            sr = new StroboRunner();
            t = new Thread(sr);
            t.start();
            return;
        }else{
            camParams.setFlashMode(Parameters.FLASH_MODE_TORCH);
        }
    }else if(!on){
        if(t != null){
            sr.stopRunning = true;
            t =null;
            return;
        }else{
            camParams.setFlashMode(Parameters.FLASH_MODE_OFF);
        }
    }
    cam.setParameters(camParams);
    cam.startPreview();
}
private class StroboRunner implements Runnable {
    boolean stopRunning = false;
    @Override
    public void run() {
        Camera.Parameters paramsOn = cam.getParameters();
        Camera.Parameters paramsOff = camParams;
        paramsOn.setFlashMode(Parameters.FLASH_MODE_TORCH);
        paramsOff.setFlashMode(Parameters.FLASH_MODE_OFF);
        try {
            while (!stopRunning) {
                cam.setParameters(paramsOn);
                cam.startPreview();
                Thread.sleep((100 - freq)*1);
                cam.setParameters(paramsOff);
                cam.startPreview();
                Thread.sleep((100 - freq)*1);

            }
        }catch(Exception e){
            }
        }
    }

}

The problem is rather simple but easy to overlook: You have two variables named freq , one in StroboRunner and one in MainActivity . 这个问题非常简单,但很容易忽略:您有两个名为freq变量,一个在StroboRunner ,一个在MainActivity

The one in MainActivity is always updated when the progress bar changes, but the code inside StroboRunner uses the local freq variable. 进度栏更改时, MainActivity中的那个始终会更新,但是StroboRunner的代码使用本地的freq变量。 And that one is only updated when you turn the strobe on, right after creating a new StroboRunner ( sr.freq = freq; ). 并且只有在创建新的StroboRunnersr.freq = freq; )之后打开频闪灯时才会更新该sr.freq = freq; That's why it works again after you turn the strobe off and on again. 这就是为什么在关闭并重新打开闪光灯后,它又可以工作的原因。

If I'm not completely mistaken, you should be able to fix this by just getting rid of the freq variable inside the StroboRunner class and the line sr.freq = freq; 如果我没有完全弄错,您应该能够通过摆脱StroboRunner类内部的freq变量和sr.freq = freq;这一行来解决此问题sr.freq = freq;

I finally found out how to fix it. 我终于找到解决方法。 Now it works perfect (Flashlight with Strobe effect, also works when the screen is locked!) 现在可以正常使用(具有频闪效果的手电筒,在屏幕锁定时也可以使用!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM