Android学习笔记(六) 调整手机屏幕亮度Brightness seekbar实现

brightness(亮度)是window中的一个属性

 

修改亮度需要取得window属性

方法为:

 WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); layoutParams.screenBrightness = b;// b是亮度 getWindow().setAttributes(layoutParams);

 

b是一个浮点数 从0~1 ,表示亮度

 

例子效果图(在真机上有效运行)

 

 Android学习笔记(六) 调整手机屏幕亮度Brightness seekbar实现_第1张图片

例子代码:

 package young.ScreenBrightnessSolution; import android.app.Activity; import android.os.Bundle; import android.view.WindowManager; import android.widget.SeekBar; import android.widget.TextView; import android.widget.SeekBar.OnSeekBarChangeListener; public class ScreenBrightnessSolution extends Activity { private SeekBar ScreenBrightnessSeekBar; private TextView tView ; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); //initView tView = (TextView)findViewById(R.id.text); ScreenBrightnessSeekBar = (SeekBar)findViewById(R.id.seekBrightness); //亮度是从0~1中的一个浮点数,由于getProess得到的是Int型,所以扩大10倍,赋值时除以10 ScreenBrightnessSeekBar.setMax(10); ScreenBrightnessSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub setScreenBrightness((float)seekBar.getProgress()/10); } public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub } }); } private void setScreenBrightness(float b){ //取得window属性保存在layoutParams中 WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); layoutParams.screenBrightness = b;//b已经除以10 getWindow().setAttributes(layoutParams); //显示修改后的亮度 layoutParams = getWindow().getAttributes(); tView.setText(String.valueOf(layoutParams.screenBrightness)); } }

 

 

你可能感兴趣的:(android,service,Class,手机,float)