效果:启动Activity,按加减图标,按下加减时图标变暗,增加减少屏幕亮度值
1.Activity
public class BrightnessSettingActivity extends BaseActivity {
private Button btadd,btdrop;
private ImageView brightnessImageValue;
int currentBrightnessValue;
ContentResolver resolver;
int maxValue;
int scale;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
if (!Settings.System.canWrite(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + this.getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
} else {
initView();
}
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
}
private void initView() throws Settings.SettingNotFoundException {
setContentView(R.layout.activity_brightness_setting);
btadd=(Button) findViewById(R.id.add_brightness);
btdrop=(Button)findViewById(R.id.drop_brightness);
brightnessImageValue=(ImageView)findViewById(R.id.brightness_value);
currentBrightnessValue = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
try {
Class<?> powerManagerClass = Class.forName("android.os.PowerManager");
Field field = powerManagerClass.getDeclaredField("BRIGHTNESS_ON");
maxValue = (int) field.get(null);
scale = maxValue/4;
} catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
// 处理异常
}
Log.e("xwd","current value:"+currentBrightnessValue);
Log.e("xwd","max value:"+maxValue);
Log.e("xwd","scale value:"+scale);
// 获取 ContentResolver
resolver = getContentResolver();
btadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
currentBrightnessValue = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
if ((currentBrightnessValue+scale)<maxValue) {
currentBrightnessValue+=scale;
}
else
{
currentBrightnessValue=maxValue;
}
setBrightnessValue(currentBrightnessValue);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
}
});
btdrop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
currentBrightnessValue = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
if ((currentBrightnessValue-scale)>0) {
currentBrightnessValue -= scale;
}
else
{
currentBrightnessValue = 0;
}
setBrightnessValue(currentBrightnessValue);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
}
});
}
private void setBrightnessValue(int value)
{
// 修改系统亮度设置(需要 WRITE_SETTINGS 权限)
Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, value);
// 应用修改后的设置
Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
}
2.布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/titleHeight"
android:orientation="horizontal">
<TextView
android:id="@+id/brightness_titleview"
android:layout_width="wrap_content"
android:layout_height="@dimen/titleHeight"
android:gravity="center|center_horizontal"
android:layout_weight="1"
android:paddingLeft="@dimen/listpadding"
android:text="@string/brightness"
android:textColor="@color/white"
android:textSize="@dimen/title_font_size" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="horizontal"
android:layout_marginTop="50dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/brightness_value"
android:src="@drawable/brightness"
>
</ImageView>
</LinearLayout>
<RelativeLayout
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/drop_brightness"
android:background="@drawable/button_drop"
android:layout_alignParentLeft="true"
android:layout_marginLeft="100dp"
>
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/add_brightness"
android:background="@drawable/button_add"
android:layout_alignParentRight="true"
android:layout_marginRight="100dp"
>
</Button>
</RelativeLayout>
</LinearLayout>
3.button增减图标按下效果添加
//button_add.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<bitmap android:src="@drawable/volume_add_down" />
</item>
<item>
<bitmap android:src="@drawable/volume_add" />
</item>
</selector>
//button_drop.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<bitmap android:src="@drawable/volume_drop_down" />
</item>
<item>
<bitmap android:src="@drawable/volume_drop" />
</item>
</selector>
4.运行效果图