调节的关键代码:

        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = Float.parseFloat(brightNum);
getWindow().setAttributes(layoutParams);

以下是一个例子,包含拖动条控件:

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
> <EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入亮度(0-1)"
/> <Button
android:id="@+id/btn_set"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="设置一下当前亮度"
android:onClick="changeBright"
/> <Button
android:id="@+id/btn_set"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="设置一下当前亮度 [0, 1]"
android:onClick="changeBright2"
/> <Button
android:id="@+id/btn_current"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="获取当前亮度(非自动调节模式生效)"
android:onClick="showCurrentBright"
/> <SeekBar
android:id="@+id/seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="设置为自动调节屏幕亮度"
android:onClick="setAutoBright"
/> <Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="设置为手动调节屏幕亮度"
android:onClick="setManualBright"
/> <Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="获取亮度的模式(与系统设置相关)"
android:onClick="showScreenMode"
/> <Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="让手机直接黑屏..."
android:onClick="setNoBright"
/>
</LinearLayout>

Activity:

package com.example.screenBrightnessTest;

import android.app.Activity;
import android.content.ContentResolver;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Toast; public class MyActivity extends Activity {
private EditText editText;
private SeekBar seekBar; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); editText = (EditText) findViewById(R.id.et);
seekBar = (SeekBar) findViewById(R.id.seek_bar); //设置滚动条
seekBar.setProgress(getCurrentBrightValue());
seekBar.setMax(255);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
/**
* 拖动中数值的时候
* @param fromUser 是否是由用户操作的
*/
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (progress > 3 && fromUser) {//以免太暗
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = (float) progress / 255;//因为这个值是[0, 1]范围的
getWindow().setAttributes(layoutParams);
}
} /**
* 当按下的时候
*/
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
System.out.println("com.example.screenBrightnessTest.MyActivity.onStartTrackingTouch");
} /**
*当松开的时候
*/
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
System.out.println("com.example.screenBrightnessTest.MyActivity.onStopTrackingTouch");
}
});
} public void changeBright(View view) {
String brightNum = editText.getText().toString();
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = Float.parseFloat(brightNum);
getWindow().setAttributes(layoutParams);
} /**
* 仅当系统的亮度模式是非自动模式的情况下,获取当前屏幕亮度值[0, 255].
* 如果是自动模式,那么该方法获得的值不正确。
*/
private int getCurrentBrightValue() {
int nowBrightnessValue = 0;
ContentResolver resolver = getContentResolver();
try {
nowBrightnessValue = android.provider.Settings.System.getInt(resolver,
Settings.System.SCREEN_BRIGHTNESS, 255);
} catch (Exception e) {
e.printStackTrace();
}
return nowBrightnessValue;
} /**
* 获取当前的亮度
*/
private float getMaxBrightValue2() {
return getWindow().getAttributes().screenBrightness;
} public void showCurrentBright(View view) {
Toast.makeText(this, "当前系统亮度的值为:" + getCurrentBrightValue(), 1).show();
} /**
* 让手机直接黑屏
*/
public void setNoBright(View view) {
getWindow().getAttributes().screenBrightness = 0;
} /**
* 设置当前屏幕亮度的模式
*
* @param paramInt Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC或者Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
*/
private void setScreenMode(int paramInt) {
try {
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, paramInt);
} catch (Exception localException) {
localException.printStackTrace();
}
} /**
* 设置为自动调节亮度
* 如果当前系统的模式不是自动调节模式,那么才会生效
*/
public void setAutoBright(View view) {
setScreenMode(Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
} /**
* 获得当前屏幕亮度的模式
*/
public void showScreenMode(View view) {
int screenMode = 0;
try {
screenMode = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);
switch (screenMode) {
case Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC:
Toast.makeText(this, "自动亮度模式", 1).show();
break;
default:
Toast.makeText(this, "手动调节亮度", 1).show();
}
} catch (Exception e) {
e.printStackTrace();
}
} public void setManualBright(View view) {
setScreenMode(Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
} public void changeBright2(View view) {
Toast.makeText(this, "亮度值[0, 1] = " + getWindow().getAttributes().screenBrightness, 1).show();
} }

Android 调节当前Activity的屏幕亮度的更多相关文章

  1. 设置当前Activity的屏幕亮度

    设置当前的Activity的屏幕亮度,而不是设置系统的屏幕亮度,退出当前的Activity后恢复系统的亮度. 直接看代码好了 Java代码 WindowManager.LayoutParams lp  ...

  2. Android中获取并设置屏幕亮度

    最近在做一个Demo的时候用到了调节屏幕亮度的功能,于是上网搜索了一下,并且写了一个小Demo测试了一下,发现代码还是比较简单的.Android中的亮度调节,主要有三个方向,一个是针对于系统的亮度调节 ...

  3. ubuntu 12.04亮度无法调节和无法保存屏幕亮度解决办法(echo_brightness)

    经过多次更改失败重装后终于在官网的answers找到了解决办法:原文链接 http://askubuntu.com/questions/3841/desktop-doesnt-remember-bri ...

  4. Android开发调节屏幕亮度

    在播放器,我们经常看到这样的设计,即,在用户的特定部分将能够滑动屏幕向上或向下调整屏幕的亮度,上下滑动的某一部分将能够调整播放音量.并以滑动的进程可以进行调整,以玩. 如今,我不得不说一下亮度调节. ...

  5. Android屏幕亮度调节相关源码

    如下代码内容是关于Android屏幕亮度调节相关的代码. public static boolean isAutoBrightness(ContentResolver aContentResolver ...

  6. android调节屏幕亮度

    一:只改变当前程序android屏幕亮度(1)方法:lp.screenBrightness 取值 0.0 -- 1.0 ※设定值(float)的范围,默认小于 0(系统设定).0.0(暗)-1.0(亮 ...

  7. Android 播放电影时滑动屏幕调整屏幕亮度(转)

    (转自:http://blog.csdn.net/piaozhiye/article/details/6544450) 发现有一些主流的播放器播放电影时可以通过滑动屏幕调整屏幕亮度,其实实现起来也很容 ...

  8. 降低屏幕亮度,减缓眼疲劳 (linux/windows/firefox/android)

    Linux 在Linux上自动调整屏幕亮度来保护眼睛 - 51CTO.COM -- 介绍了Camera和RedShift这两款工具 How to automatically dim your scre ...

  9. Android 高级UI设计笔记23:Android 夜间模式之 两种常用方法(降低屏幕亮度+替换theme)

    1. 夜间模式 所谓的夜间模式,就是能够根据不同的设定,呈现不同风格的界面给用户,而且晚上看着不伤眼睛.特别是一些新闻类App实现夜间模式是非常人性化的,增强用户体验. 2. 我根据网上的资料 以及自 ...

随机推荐

  1. 在线生成ICO图标、站标

    网上一搜有很多,找了两个比较好用的,分别是http://ico.storyren.com/和http://www.ico.la/,前面的那个好像更好点.上传png.jpg.或gif格式的图片,按自己需 ...

  2. Windows Phone 中查找可视化树中的某个类型的元素

    private void StackPanel_Tap(object sender, TappedRoutedEventArgs e) { //获取到的对象是ListBoxItem ListBoxIt ...

  3. 关于SQLite的创建以及使用相关说明

    关于SQLite的创建以及使用相关说明 没有给出具体的程序,但看完这后可能对你有所帮助. 数据库操作基本知识: execSQL(String sql): 执行一个数据库语句 insert(table, ...

  4. dotNet中初始化器的使用

    dotNet中初始化器的使用 2013年12月7日 13:27 有两类初始化器: 对象初始化器和集合初始化器 比如现在有一个User类: Public   class User { public in ...

  5. grappelli美化django的admin页面

    开始用admin时候,觉得它的页面实在...宁愿自己写modules,多费点时间 grappelli可以把admin变得非常美观,配置起来也很简单 第一步,先下载grappelli,搜索一下,wind ...

  6. Excel REPT函数使用

    需要制作1K大小的数据 使用Excel REPT函数可以迅速制造 Excel REPT 函数 =REPT(1,1024) 结果直接黏贴进txt文件,注意删除尾空格.

  7. MySQL 简洁连接数据库方式

    OS  :   CentOS 6.3 DB  :  5.5.14 MySQL连接数据库的方式很多: 1.[root@db01 bin]# ./mysql -uroot -p 2.[root@db01 ...

  8. vc2005 编译ACE-6.2.0

    vc2005 编译ACE-6.2.0 下载并解压ACE-6.2.0 ftp://download.dre.vanderbilt.edu/previous_versions/ACE-6.2.0.zip ...

  9. WebAPi(selfhost)

    ASP.NET WebAPi(selfhost)之文件同步或异步上传   前言 前面我们讲过利用AngularJs上传到WebAPi中进行处理,同时我们在MVC系列中讲过文件上传,本文结合MVC+We ...

  10. C# 获取数组的子集

    private static void PrintSubItems(int[] source) { int i = 1; int total = int.Parse(Math.Pow(2, sourc ...