打算做一款阅读软件,其中有涉及到调节亮度的,于是花了点时间研究了下关于亮度调节的东西,其实也是参考别人的,
思路:想要调节软件的亮度,必须要有获得系统配置的权限,所以先添加权限
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
添加权限之后就可以进行相关的操作了
废话不多说,直接贴代码
1.关于亮度控制的类
package com.example.brightness;
import android.app.Activity;
import android.content.ContentResolver;
import android.provider.Settings;
import android.view.WindowManager;
/**
* 用于亮度调节
*
* @author huarizhong
*
*/
public class LightnessControl {
/**
* 是否为自动调节亮度
*
* @param activity
* @return
*/
public static boolean isAuto(Activity activity) {
boolean autoBrightness = false;
//使用ContentResolver获得系统的数据
ContentResolver resolver = activity.getContentResolver();
try {
//判断是否为自动调节
autoBrightness = Settings.System.getInt(resolver,
Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
} catch (Exception e) {
e.printStackTrace();
}
return autoBrightness;
}
/**
* 设置亮度值
*/
public static void setLightness(Activity activity, int value) {
try {
Settings.System.putInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, value);
WindowManager.LayoutParams lp = activity.getWindow()
.getAttributes();
lp.screenBrightness = (value <= 0 ? 1 : value) / 255f;
activity.getWindow().setAttributes(lp);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取亮度
*
* @param act
* @return
*/
public static int getLightness(Activity act) {
return Settings.System.getInt(act.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, -1);
}
/**
* 停止自动亮度调节
*
* @param activity
*/
public static void stopAutoBrightness(Activity activity) {
Settings.System.putInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
/**
* 开启亮度自动调节
*
* @param activity
*/
public static void startAutoBrightness(Activity activity) {
Settings.System.putInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
}
}
2.在Activity中的调用
package com.example.brightness;
import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity {
// 调节亮度的进度条
private SeekBar seekBar;
// 系统自身亮度
int sysBrightness;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获得系统亮度
sysBrightness = LightnessControl.getLightness(this);
seekBar = (SeekBar) findViewById(R.id.seekBar1);
seekBar.setProgress(sysBrightness);
// 监听进度条的变化
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
//进度条发生变化调节亮度
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
LightnessControl.setLightness(MainActivity.this, progress);
}
});
}
}
3.XML布局文件
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:max="255"
android:layout_marginTop="92dp" />
</RelativeLayout>
如果需要demo的可以留下邮箱