屏幕亮度显示的控制

1.  SeekBarDemo.java

public class SeekBarDemo extends AppCompatActivity {
    private SeekBar seekBar = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seek_bar_demo);
        this.seekBar = (SeekBar)findViewById(R.id.seekbar);  //取得seekBar
        this.seekBar.setMax(100);  //以后计算的时候除以100
        this.seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListenerImpl());
    }

    private class OnSeekBarChangeListenerImpl implements SeekBar.OnSeekBarChangeListener {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            //正在拖动
        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            //开始拖动
        }
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {  //停止拖动
            SeekBarDemo.this.setScreenBrightness((float)seekBar.getProgress()/100);  //计算出当前值
        }
    }

    private void setScreenBrightness(float num) {
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();  //取得windows属性
        layoutParams.screenBrightness = num;   //num已经除以100
        super.getWindow().setAttributes(layoutParams);  //0~1之间
    }
}

2.  activity_seek_bar_demo.xml

<SeekBar
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/seekbar"/>
<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/img"
    android:src="@drawable/dog1"
    android:layout_below="@+id/seekbar"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="55dp" />

    由于亮度只能在0~1之间的数字表示,所以在每次拖动的时候都要将当前的数字除以100,之后将此内容设置到window的screenBrightness属性中。

你可能感兴趣的:(android,屏幕亮度)