2014-04-26 80 views
1

我想通过单击按钮来调整屏幕亮度,所以当背景为白色时,屏幕亮度应该最大,同时如果背景为黑色,则屏幕亮度应该最小,但是出现错误:NullPointerException ... 这里是我的代码:如何设置屏幕亮度?

public void lamp2(boolean mode){ 

     if(mode){ 

      r.setBackgroundColor(Color.WHITE); 
      btn.setText("Turn OFF"); 
      btn.setTextColor(Color.RED); 
      WindowManager.LayoutParams lp = getWindow().getAttributes(); 
      lp.screenBrightness = 90/100.0f; 
      getWindow().setAttributes(lp); 
      this.mode = true; 
     } 

     else if(!mode){ 

      r.setBackgroundColor(Color.BLACK); 
      btn.setText("Turn ON"); 
      btn.setTextColor(Color.GREEN); 
      WindowManager.LayoutParams lp = getWindow().getAttributes(); 
      lp.screenBrightness = 100/100.0f; 
      getWindow().setAttributes(lp); 
      this.mode = false; 
     } 
    } 
+0

你在哪里得到* NullPointerException *? –

+0

@MatejSpili我解决了这个问题,但我怎样才能获得最大的亮度和最低? –

回答

5

把这些线以获得最大的

WindowManager.LayoutParams params = getWindow().getAttributes(); 
params.screenBrightness = 1.0f; 
getWindow().setAttributes(params); 

把这些线以获得最低

WindowManager.LayoutParams params = getWindow().getAttributes(); 
params.screenBrightness = 0.1f; 
getWindow().setAttributes(params); 
+0

在发布您的应用程序之前阅读此答案http://stackoverflow.com/a/9703871/1972566 – Ravi