android调节当前屏幕亮度的实现方式
android调节当前屏幕亮度;获取系统屏幕亮度;seekbar拖动条1、效果图2、布局文件<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android
·
我们平时使用的一些应用可能会有调节当前屏幕亮度却不改变系统亮度的功能,比如小说APP在阅读电子书时就拥有调节亮度的功能,但这个功能却在退出阅读模式后就失效了,这是怎么实现的?
以下就是调节当前屏幕亮度的实现方式:
1、效果图
2、布局文件
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="水平滑动调节亮度"
android:textSize="20sp" />
<SeekBar
android:id="@+id/seekbar_light"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:max="255" />
<TextView
android:id="@+id/tv_light_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
3、工具类
public class BrightnessUtil {
/**
*获取系统屏幕亮度
*/
public static int getBrightness(Context context) {
int brightness = -1;
ContentResolver resolver = context.getContentResolver();
try {
brightness = Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return brightness;
}
/**
*获取系统最大屏幕亮度
*/
public static int getMaxBrightness(Context context) {
int brightnessSettingMaximumId = context.getResources().getIdentifier("config_screenBrightnessSettingMaximum", "integer", "android");
int brightnessSettingMaximum = context.getResources().getInteger(brightnessSettingMaximumId);
return brightnessSettingMaximum;
}
/**
*调节当前屏幕亮度
*/
public static void SetSystemLight(int lightnumber, Activity activity){
Window window = activity.getWindow();//对当前窗口进行设置
WindowManager.LayoutParams layoutparams = window.getAttributes();//获取窗口属性为后面亮度做铺垫作用
layoutparams.screenBrightness =lightnumber / 255.0f;//用窗口管理(自定义的)layoutparams获取亮度值,android亮度值处于在0-255之间的整形数值
window.setAttributes(layoutparams);//设置当前窗口屏幕亮度
}
}
4、实现类(活动)
public class MainActivity extends AppCompatActivity {
private static final String TAG ="MainActivity" ;
private TextView textView;
private SeekBar seekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initBrightness();
/* 监听SeekBar */
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
/*拖动条停止拖动时调用 */
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//Log.i(TAG, "拖动停止");
}
/*拖动条开始拖动时调用*/
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
//Log.i(TAG, "开始拖动");
}
/* 拖动条进度改变时调用*/
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
textView.setText("亮度值:" + progress);
//当前屏幕亮度随着进度值改变
BrightnessUtil.SetSystemLight(progress,MainActivity.this);
}
});
}
/* 初始化控件 */
private void initBrightness(){
textView = (TextView) findViewById(R.id.tv_light_value);
seekBar = (SeekBar) findViewById(R.id.seekbar_light);
textView.setText("亮度值:" + BrightnessUtil.getBrightness(MainActivity.this));
//将系统最大屏幕亮度值设为seekbar的最大进度值
seekBar.setMax(BrightnessUtil.getMaxBrightness(MainActivity.this));
//将系统当前屏幕亮度值设为seekbar当前进度值
seekBar.setProgress(BrightnessUtil.getBrightness(MainActivity.this));
}
}
调节系统亮度的方法:
https://blog.csdn.net/jppipai/article/details/121051328
更多推荐
已为社区贡献9条内容
所有评论(0)