Android滑动手势调节屏幕亮度

金卡戴珊uglyIP属地: 天津
字数 109

android系统提供了api调整当前窗口亮度,或者调节系统亮度。
根据使用场景出发,只需要改变视频播放窗口亮度即可。退出窗口恢复系统亮度。
为了优化体验,需要在视频播放activity创建时,获取当前系统亮度。设置到滑动控制的
progress刻度中。代码如下:


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
var bri = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS)

var percent = bri * 100 / 255
        val seekbar = findViewById<SeekBar>(R.id.seekbar)
        val text = findViewById<TextView>(R.id.tv)
        seekbar.progress = percent
        seekbar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                windowBrightness = progress.toFloat() / 100F
                text.text = "当前窗口亮度=$windowBrightness"
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
            }
        })
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="拖动改变窗口亮度"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

    <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content"
             app:layout_constraintTop_toBottomOf="@+id/tv"
             android:layout_margin="10dp"
             android:id="@+id/seekbar"
    />
</androidx.constraintlayout.widget.ConstraintLayout>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
0人点赞
总资产1共写了7918字获得9个赞共4个粉丝

推荐阅读更多精彩内容