实现Android 使用代码调节屏幕亮度,
实现自动跟随系统亮度。
1,实现效果:
image2,实现代码
【1】XML文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/tv"
android:layout_below="@+id/seek"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动调节亮度"/>
<SeekBar
android:id="@+id/seek"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/cb"
android:layout_below="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跟随系统亮度"/>
</RelativeLayout>
【2】代码实现:
package com.xiaoshuai.www.luminancedemo;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
private SeekBar seekBar;
private CheckBox cb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initEvent();
}
/**
* 初始化监听
*/
private void initEvent() {
//设置seekBar进度被改变的时候的时间监听
seekBar.setOnSeekBarChangeListener(new MyOnSeekBarChangeListener());
//设置CheckBox的点选监听事件
cb.setOnCheckedChangeListener(this);
}
/**
* 初始化控件的一些操作
*/
private void initView() {
seekBar = (SeekBar) findViewById(R.id.seek);
cb = (CheckBox) findViewById(R.id.cb);
//设置最大刻度
seekBar.setMax(255);
//设置初始的Progress
seekBar.setProgress(getSystemBrightness());
//出世设置checkBox为选中状态
cb.setChecked(true);
//设置初始的屏幕亮度与系统一致
changeAppBrightness(getSystemBrightness());
}
/**
* 获得系统亮度
*
* @return
*/
private int getSystemBrightness() {
int systemBrightness = 0;
try {
//获取系统当前的屏幕的亮度
systemBrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return systemBrightness;
}
/**
* 改变App当前Window亮度
*
* @param brightness
*/
public void changeAppBrightness(int brightness) {
Window window = this.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
if (brightness == -1) {
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
} else {
//需要注意的是,返回的亮度是介于0~255之间的int类型值(也是为什么我们将seekBar的MaxValue设置为255的原因)
lp.screenBrightness = (brightness <= 0 ? 1 : brightness) / 255f;
}
window.setAttributes(lp);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(this, getSystemBrightness() + "", Toast.LENGTH_SHORT).show();
changeAppBrightness(getSystemBrightness());
} else {
int seekBarProgress = seekBar.getProgress();
changeAppBrightness(seekBarProgress);
}
}
class MyOnSeekBarChangeListener implements SeekBar.OnSeekBarChangeListener {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//seekBar进度条被改变的时候取消checkBox的点选
cb.setChecked(false);
//改变亮度
changeAppBrightness(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
}
网友评论