当前位置:   article > 正文

Android 调节屏幕亮度问题_android params.screenbrightness失效

android params.screenbrightness失效

研究调节屏幕亮度时遇到的问解决方案如下:

1、可以通过获取当前Activity 的WindowManager.LayoutParams 属性,然后设置其screenBrightness的值即可;

screenBrightness 的取值范围在[0,1]之间,具体可以如下操作:

  1. WindowManager.LayoutParams lp = getWindow().getAttributes();
  2. lp.screenBrightness = 0.5;
  3. getWindow().setAttributes(lp);

2、有时我们需要用一个Seekbar控件来动态控制屏幕的亮度。

  首先获取系统屏幕的最大值,然后设置seekbar的setMax。

  1. /**
  2. * 获取屏幕亮度
  3. */
  4. private int getScreenBrightness() {
  5. int nowBrightnessValue = 0;
  6. ContentResolver resolver = getContentResolver();
  7. try {
  8. nowBrightnessValue = android.provider.Settings.System.getInt(resolver,
  9. Settings.System.SCREEN_BRIGHTNESS);
  10. } catch (Exception e) {
  11. e.printStackTrace();
  12. }
  13. return nowBrightnessValue;
  14. }


然后在调节屏幕亮度函数中与seekbar进行绑定

  1. private void setScreenLight(View popupView) {
  2. mBrightnessProgress = getScreenBrightness();
  3. final SeekBar mSeekBarLight = (SeekBar) popupView.findViewById(R.id.seekBarLight);
  4. mSeekBarLight.setProgress(mBrightnessProgress);
  5. mSeekBarLight.setMax(MAX_BRIGHTNESS);
  6. mSeekBarLight.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
  7. @Override
  8. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  9. // TODO Auto-generated method stub
  10. if (fromUser) {
  11. WindowManager.LayoutParams lp = getWindow().getAttributes();
  12. lp.screenBrightness = (float) (progress / (MAX_BRIGHTNESS * 1.0));
  13. getWindow().setAttributes(lp);
  14. }
  15. }
  16. @Override
  17. public void onStartTrackingTouch(SeekBar arg0) {
  18. // TODO Auto-generated method stub
  19. }
  20. @Override
  21. public void onStopTrackingTouch(SeekBar arg0) {
  22. // TODO Auto-generated method stub
  23. }
  24. });
}

3、当我们遇到把Activity做为子Activity潜入到TabActivity 或者 ViewGroup 类容器时,通常上面的方法设置无法取得成功,

活动滚动条的过程中,屏幕亮度不发生改变。

因为调节亮度作用域发生了改变,之前是在Activity里面对亮度修改, 而现在是作为子activity对TabActivity或ViewGroup 容器进行修改,因此不能成功,需要

通过getParent()方法获取器Parent,然后设置。

  1. private void setScreenLight(View popupView) {
  2. mBrightnessProgress = getScreenBrightness();
  3. final SeekBar mSeekBarLight = (SeekBar) popupView.findViewById(R.id.seekBarLight);
  4. mSeekBarLight.setProgress(mBrightnessProgress);
  5. mSeekBarLight.setMax(MAX_BRIGHTNESS);
  6. mSeekBarLight.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
  7. @Override
  8. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  9. // TODO Auto-generated method stub
  10. if (fromUser) {
  11. WindowManager.LayoutParams lp = getParent().getWindow().getAttributes();
  12. lp.screenBrightness = (float) (progress / (MAX_BRIGHTNESS * 1.0));
  13. getParent().getWindow().setAttributes(lp);
  14. }
  15. }
  16. @Override
  17. public void onStartTrackingTouch(SeekBar arg0) {
  18. // TODO Auto-generated method stub
  19. }
  20. @Override
  21. public void onStopTrackingTouch(SeekBar arg0) {
  22. // TODO Auto-generated method stub
  23. }
  24. });
  25. }



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/642995
推荐阅读
相关标签
  

闽ICP备14008679号