SeekBar进度条滑动调节屏幕亮度

1.布局文件



    
    

2:布局文件所需的po_seekbar和shape_circle
po_seekbar文件




    
        
            
            
        
    
    
        
            
                
                
            
        
    
    
        
            
                
                
            
        
    


shape_circle文件




    
    
    
    
    

    

3:主代码

public class MainActivity extends AppCompatActivity {
    //声明拖动条
    private SeekBar seekBar;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.textview);
        seekBar = (SeekBar) findViewById(R.id.progress);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                //当滑动条的位置发生改变时,触发该方法,在这里直接调用参数progress,即当前滑块代表的进度值
                textView.setText("Value" + Integer.toString(progress));
                MainActivity.this.setScreenBrightness((float) seekBar.getProgress() / 100);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    private void setScreenBrightness(float num) {
        //取得屏幕的属性
        WindowManager.LayoutParams layoutParams = super.getWindow().getAttributes();
        //设置屏幕的亮度
        layoutParams.screenBrightness = num;
        //重新设置窗口的属性
        super.getWindow().setAttributes(layoutParams);
    }

}

你可能感兴趣的:(SeekBar进度条滑动调节屏幕亮度)