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>