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.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="15dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"/>
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />
</LinearLayout>
3、工具类
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import android.view.Window;
import android.view.WindowManager;
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;
window.setAttributes(layoutparams);
}
}
4、实现类(活动)
import android.os.Bundle;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
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.main_activity);
initBrightness();
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.textView);
seekBar = (SeekBar) findViewById(R.id.seekBar);
textView.setText("亮度值:" + BrightnessUtil.getBrightness(MainActivity.this));
seekBar.setMax(BrightnessUtil.getMaxBrightness(MainActivity.this));
seekBar.setProgress(BrightnessUtil.getBrightness(MainActivity.this));
}
}
参考:https://blog.csdn.net/Jz_Panda/article/details/104441904 转载请注明出处~
|