停止更改背景颜色

时间:2018-10-21 16:23:27

标签: android android-fragments morse-code

我正在创建一个多色莫尔斯应用程序。它的工作相对较好。亮度方法可以按预期工作,但是当我尝试将其停止时,背景颜色会不断变化,并且不会停止。

    iv_flashoff.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            iv_flashon.setVisibility(View.VISIBLE);
            iv_flashoff.setVisibility(View.GONE);

            int brightness = 255;
            WindowManager.LayoutParams layoutpars = window.getAttributes();
            layoutpars.screenBrightness = brightness / (float) 255;
            window.setAttributes(layoutpars);
            morseRenderer=new MorseRd();
            // start the sosWorker.
            SosWorker.Instance().execute();
            startFlashSos();
        }
    });

            iv_flashon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    iv_flashoff.setVisibility(View.VISIBLE);
                    iv_flashon.setVisibility(View.GONE);

                    int curBrightnessValue = android.provider.Settings.System.getInt(cResolver,
                            android.provider.Settings.System.SCREEN_BRIGHTNESS,-1);
                    WindowManager.LayoutParams layoutpars = window.getAttributes();
                    layoutpars.screenBrightness = curBrightnessValue;
                    window.setAttributes(layoutpars);
                    SosWorker.Instance().stop();

                    stopFlashSos();
                    myTimer.cancel();

        }
    });

在单击“ iv_flashoff”时,代码将更改屏幕亮度值并根据计时器值开始更改颜色。在单击“ iv_flashon”代码时,再次将其更改为较低值,并且还应该停止更改背景值和计时器。

  private class MorseRd implements MorseRenderer {

    @Override
    public void render(MorseAction.morseAction morseAction) {
        myTimer = new Timer();
        switch (morseAction) {

            case LOUD:
                discoScreen.setBackgroundColor(images[getRandomNumber()]);
                myTimer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        TimerMethod();
                    }
                }, 0, 8000);
                break;
            case SILENCE:

                discoScreen.setBackgroundColor(images[getRandomNumber()]);
                myTimer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        TimerMethod();
                    }
                }, 0, 8000);
                break;
        }
    }
}

private void startFlashSos(){
    SosWorker.Instance().addRenderer(morseRenderer);
}
private void stopFlashSos(){
    SosWorker.Instance().removeRenderer(morseRenderer);
}

private int getRandomNumber() {
    return new Random().nextInt(4);
}
private void TimerMethod() {
    handler = new Handler(Looper.getMainLooper());
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            discoScreen.setBackgroundResource(images[getRandomNumber()]);
        }
    }, 3000);
};

0 个答案:

没有答案