当前位置:   article > 正文

SeekBar进度条滑动调节屏幕亮度_android 设置进度条亮度值

android 设置进度条亮度值

1.布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">
    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <SeekBar
        android:id="@+id/progress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:progressDrawable="@drawable/po_seekbar"
        android:thumb="@drawable/shape_circle"

        />
</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

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

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@android:id/background">
        <shape>
            <solid android:color="#C9CAC5" />
            <corners android:radius="10dip"/>
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="10dip"/>
                <solid android:color="#C9CAC5" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="10dip"/>
                <solid android:color="#FF5722" />
            </shape>
        </clip>
    </item>

</layer-list>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

shape_circle文件

<?xml version="1.0" encoding="utf-8"?>
<!--seekbar的滑动块样式-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <!--solid表示远的填充色-->
    <solid android:color="#484848"></solid>
    <!--stroke代表远的边框线-->
    <stroke android:color="#484848"
        android:width="1dp"
        ></stroke>
    <!--size控制高宽-->

    <size
        android:width="24dp"
        android:height="24dp"></size>
</shape>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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);
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签