2012-06-20 107 views
0

我的應用程序是一個簡單的「亮度轉換器」,這也改變系統亮度(什麼是不容易實現:D)。
我實際上設置了一切和它的工作正常...除了一個小煩人的事情...每次亮度改變屏幕首先「閃爍」到最高亮度(如100%的光),並在半秒鐘左右,它變成正確的亮度。閃存,同時更改系統亮度

示例:系統亮度應爲120:(最大)1.轉到255,adter半秒變化到120

這是我的兩個類的代碼。虛擬類用於「重置」(對於應用亮度變化所必需的)。

編輯:順便說一句。我認爲問題是「lp.screenBrightness = 100/100.0f;」在虛擬活動中。但它不會沒有它的工作:/


test.java

public class Test extends Activity 
{ 
    SeekBar seekbar; 
    TextView value; 
    TextView debug; 
    Button button1; 
    Button button2; 
    Button button3; 

    public void onCreate(Bundle savedInstanceState) 
    { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     value = (TextView) findViewById(R.id.textview2); 
     debug = (TextView) findViewById(R.id.textview3); 
     seekbar = (SeekBar) findViewById(R.id.seekbar); 
     button1 = (Button) findViewById(R.id.button1); 
     button2 = (Button) findViewById(R.id.button2); 
     button3 = (Button) findViewById(R.id.button3); 

     seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
     { 
      public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) 
      { 
       // TODO Auto-generated method stub 
       int brightness = progress+1; 
       double b = brightness/2.55; 
       double c = Math.round(b*100)/100; 
       int percent = (int) c; 
       if (percent==0) { 
        value.setText("Lightlevel: !MIN!"); 
       }else if (percent==100){ 
        value.setText("Lightlevel: !MAX!"); 
       }else{ 
        value.setText("Lightlevel: "+percent+"%"); 
       } 
       debug.setText("int brightness: "+brightness+"/double b:"+b+"/double c: "+c+"/int percent: "+percent); 
       Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, brightness); 
      } 

      public void onStartTrackingTouch(SeekBar seekBar) 
      { 
       // TODO Auto-generated method stub 
       Intent in = new Intent(Test.this,DummyBrightnessActivity.class); 
       startActivity(in); 
      } 

      @Override 
      public void onStopTrackingTouch(SeekBar seekBar) { 
       // TODO Auto-generated method stub 

      } 
     }); 


     button1.setOnClickListener(new OnClickListener() 
     { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, 1); 

       Intent in = new Intent(Test.this,DummyBrightnessActivity.class); 
       startActivity(in); 
      } 

     }); 

     button2.setOnClickListener(new OnClickListener() 
     { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, 127); 

       Intent in = new Intent(Test.this,DummyBrightnessActivity.class); 
       startActivity(in); 
      } 

     }); 

     button3.setOnClickListener(new OnClickListener() 
     { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, 255); 

       Intent in = new Intent(Test.this,DummyBrightnessActivity.class); 
       startActivity(in); 
      } 

     }); 
    } 
} 

DummyBrightnessActivity.java

public class DummyBrightnessActivity extends Activity{ 

    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     //setContentView(R.layout.dummy); 

     WindowManager.LayoutParams lp = getWindow().getAttributes(); 
     lp.screenBrightness = 100/100.0f; 
     getWindow().setAttributes(lp); 

     Timer timer2 = new Timer(); 
     timer2.schedule(new TimerTask() { 

      public void run() { 

       finish(); 

      } 

     }, 500); 

    } 
} 

回答

0

回答我的問題以備將來使用。

問題確實是lp.screenBrightness = 100/100.0f;。所以我在intent中放了一些變量,它的正確值是brightness,所以我可以使它變成lp.screenBrightness = brightness;

我知道這有點簡短,但如果有人想知道更多關於它的信息,請在評論中提問。