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

本文详细介绍了如何在Android应用中自定义SeekBar组件的外观,包括进度条背景、颜色和滑块样式,并展示了如何使用SeekBar来动态调整设备的屏幕亮度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>

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>

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>

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

}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值