1.首先,我们需要一个操作屏幕亮度的类。不多说,直接上代码。
package zz.screenlight;
import android.app.Activity;
import android.content.ContentResolver;
import android.net.Uri;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.Log;
import android.view.WindowManager;
public class BrightnessTools {
/** 判断是否开启了自动亮度调节 */
public static boolean isAutoBrightness(ContentResolver aContentResolver) {
boolean automicBrightness = false;
try {
automicBrightness = Settings.System.getInt(aContentResolver,
Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
}
catch (SettingNotFoundException e)
{
e.printStackTrace();
}
return automicBrightness;
}
/** 获取屏幕的亮度 */
public static int getScreenBrightness(Activity activity) {
int nowBrightnessValue = 0;
ContentResolver resolver = activity.getContentResolver();
try {
nowBrightnessValue = android.provider.Settings.System.getInt(
resolver, Settings.System.SCREEN_BRIGHTNESS);
}
catch (Exception e) {
e.printStackTrace();
}
return nowBrightnessValue;
}
/** 设置亮度 */
public static void setBrightness(Activity activity, int brightness) {
// Settings.System.putInt(activity.getContentResolver(),
// Settings.System.SCREEN_BRIGHTNESS_MODE,
// Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);
Log.d("lxy", "set lp.screenBrightness == " + lp.screenBrightness);
activity.getWindow().setAttributes(lp);
}
/**停止自动亮度调节 */
public static void stopAutoBrightness(Activity activity) {
Settings.System.putInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
/**开启亮度自动调节 */
public static void startAutoBrightness(Activity activity) {
Settings.System.putInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
}
/** 保存亮度设置状态 */
public static void saveBrightness(ContentResolver resolver, int brightness) {
Uri uri = android.provider.Settings.System
.getUriFor("screen_brightness");
android.provider.Settings.System.putInt(resolver, "screen_brightness",
brightness);
// resolver.registerContentObserver(uri, true, myContentObserver);
resolver.notifyChange(uri, null);
}
}
2.把上边这个类直接放到你的utils工具类中即可使用了,他里面包含了获取当前屏幕亮度,设置亮度,等功能。
3.下面是我写的一个小demo
3.1 在activity_main.xml 中定义了几个按钮,可以点击按钮进行相关的设置,并且获取数据。
<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:padding="20dp" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="state"
android:text="屏幕亮度状态" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="close"
android:text="关闭自动亮度调节功能" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="open"
android:text="开启自动亮度调节功能" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="get"
android:text="获取当前屏幕亮度" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="set"
android:text="设置屏幕亮度" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="save"
android:text="保存屏幕亮度" />
</LinearLayout>
3.2.接下来是MainActivity.java中的代码了。
package zz.screenlight;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
private float downY;
private float moveY;
private int screenBrightness;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// 判断亮度状态
public void state(View view) {
boolean autoBrightness = BrightnessTools
.isAutoBrightness(getContentResolver());
Toast.makeText(getApplicationContext(), "亮度状态为" + autoBrightness,
Toast.LENGTH_SHORT).show();
}
// 关闭自动亮度调节功能
public void close(View view) {
BrightnessTools.stopAutoBrightness(this);
Toast.makeText(getApplicationContext(), "关闭自动亮度调节功能",
Toast.LENGTH_SHORT).show();
}
// 开启自动亮度调节功能
public void open(View view) {
BrightnessTools.startAutoBrightness(this);
Toast.makeText(getApplicationContext(), "开启自动亮度调节功能",
Toast.LENGTH_SHORT).show();
}
// 获取屏幕亮度
public void get(View view) {
int screenBrightness = BrightnessTools.getScreenBrightness(this);
Toast.makeText(getApplicationContext(), "获取屏幕亮度" + screenBrightness,
Toast.LENGTH_SHORT).show();
}
// 设置屏幕亮度,接着保存屏幕亮度
public void set(View view) {
int i = 33;
BrightnessTools.setBrightness(this, i);
Toast.makeText(getApplicationContext(), "设置屏幕亮度" + i,
Toast.LENGTH_SHORT).show();
BrightnessTools.saveBrightness(getContentResolver(), i);
}
// 保存屏幕亮度
public void save(View view) {
int brightness = 0;
BrightnessTools.saveBrightness(getContentResolver(), brightness);
Toast.makeText(getApplicationContext(), "保存屏幕亮度" + brightness,
Toast.LENGTH_SHORT).show();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downY = event.getY();
// 获取当前亮度
screenBrightness = BrightnessTools
.getScreenBrightness(MainActivity.this);
break;
case MotionEvent.ACTION_MOVE:
moveY = event.getY();
float disY = moveY - downY;
int heightPixels = getResources().getDisplayMetrics().heightPixels / 2;
float percentLight = disY / heightPixels;
// 亮度偏移量
int offsetLight = (int) (percentLight * 255);
Log.i("MainActivity", offsetLight+"");
// 最终亮度
int endLight = screenBrightness + offsetLight;
Log.i("MainActivity", endLight+"");
if (endLight >= 0 && endLight <= 255) {
BrightnessTools.setBrightness(MainActivity.this, endLight);
BrightnessTools.saveBrightness(getContentResolver(), endLight);
}
break;
case MotionEvent.ACTION_UP:
break;
default:
break;
}
return super.onTouchEvent(event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
上下滑动改变亮度是在onTouchEvent方法中来实现的,主要是通过滑动距离占屏幕高度一半的百分比计算的。至此就可以在屏幕上滑动的时候改变屏幕的亮度了。
3.3 千万记得在清单文件中添加上一个设置的权限,否则会秒退。
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
4.现在,就可把最先写的类和上下滑动的代码移植到你自己的代码中了,轻松实现滑动改变屏幕亮度。其实也可以设置成左右滑动改变亮度功能,原理相同,这里就不在写了。