一:只改变当前程序android屏幕亮度
(1)方法:
lp.screenBrightness 取值 0.0 -- 1.0 ※设定值(float)的范围,默认小于 0(系统设定)、0.0(暗)~1.0(亮) ※调用处理的地方,例如, Activity.onCreate()等等
代码:
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 1.0f;
getWindow().setAttributes(lp);
注:1、b是一个浮点数 从0~1 ,表示亮度
2、当我们遇到把Activity做为子Activity潜入到TabActivity 或者 ViewGroup 类容器时,通常上面的方法设置无法取得成功。
在子Activity中,屏幕亮度不发生改变。因为调节亮度作用域发生了改变,之前是在Activity里面对亮度修改。
而现在是作为子activity对TabActivity或ViewGroup 容器进行修改。
因此不能成功,需要通过getParent()方法获取器Parent,然后设置。
3、
(2)恢复
当离开当前Acitivity时,屏幕亮度会恢复到原先的亮度。另外将lp.screenBrightness 设为 -1(
WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE),也会让屏幕恢复到原先的亮度(即系统设置)。
(3)最小亮度
WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE,官方文档说这个值可以将屏幕亮度设置到最低亮度(Lowest Brightness)。实际意识是将屏幕设置到全黑,屏幕也无法响应触控了。
在G3(CM6)上,将屏幕设置到最低亮度值是
0.004(精度0.001),这时屏幕基本全黑,但仍能控制。低于0.004(精度0.001)时,屏幕便失去控制。0.01也是个要记录的值,屏幕亮度足够低,当仍能看到东西。这些值应该是和设备有关的,不能乱设。
(4)封装进度条控制
1、封装类:
package com.cindigo.MyView;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.preference.DialogPreference;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
/**
*
* @Cindigo HUST
*
* @author jairkong
*
* @SINCE 2012-11
*
* @模块及功能 自定义SeekBarPreference
*
**/
/*
* 自定义SeekBarPreference
*/
public class SeekBarPreference extends DialogPreference implements SeekBar.OnSeekBarChangeListener
{
private static final String androidns="http://schemas.android.com/apk/res/android";
public SeekBar mSeekBar;
private TextView mSplashText,mValueText;
private Context mContext;
private String mDialogMessage, mSuffix;
private int mDefault, mMax, mValue = 0,oldValue=0,preValue=0;
public SeekBarPreference(Context context, AttributeSet attrs) {
super(context,attrs);
mContext = context;
mDialogMessage = attrs.getAttributeValue(androidns,"dialogMessage");
mSuffix = attrs.getAttributeValue(androidns,"text");
mDefault = attrs.getAttributeIntValue(androidns,"defaultValue", 0);
mMax = attrs.getAttributeIntValue(androidns,"max", 100);
Log.e("SeekBarPreference", "SeekBarPreference1---"+"defaultValue:"+mDefault+"mDefault:"+mMax);
}
public SeekBarPreference(Context context, AttributeSet attrs,int defStyle) {
super(context, attrs, defStyle);
Log.e("SeekBarPreference", "SeekBarPreference2");
}
/**
* 函数功能:创建dialog组建
*
* @param null
*
* @return layout
*
*/
@Override
protected View onCreateDialogView() {
LinearLayout.LayoutParams params;
LinearLayout layout = new LinearLayout(mContext);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(6,6,6,6);
mSplashText = new TextView(mContext);
mSplashText.setTextColor(Color.WHITE);
mSplashText.setTextSize(20);
if (mDialogMessage != null)
mSplashText.setText(mDialogMessage);
layout.addView(mSplashText);
mValueText = new TextView(mContext);
mValueText.setGravity(Gravity.CENTER_HORIZONTAL);
mValueText.setTextSize(25);
mValueText.setTextColor(Color.WHITE);
params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
mValue = shouldPersist() ? getPersistedInt(mDefault) : 0;
if (mValueText != null){
String volum=String.valueOf(100*mValue/getMax())+"%";
mValueText.setText(mSuffix == null ? volum : mSuffix.concat(volum));
}
layout.addView(mValueText, params);
mSeekBar = new SeekBar(mContext);
//设置progressbar属性
setMax(mMax);
setProgress(mValue);
mSeekBar.setOnSeekBarChangeListener(this);
layout.addView(mSeekBar, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
Log.e("SeekBarPreference", "onCreateDialogView---getProgress():"+getProgress()+"---Max:"+mMax);
getOldValue();//得到并保存初始值
Log.e("SeekBarPreference", "onCreateDialogView---oldValue:"+oldValue);
return layout;
}
/**
* 函数功能:重写绑定视图函数
*
* @param
*
* @return null
*
*/
@Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
//设置progressbar属性
setMax(mMax);
setProgress(mValue);
Log.e("SeekBarPreference", "onBindDialogView---mValue:"+mValue);
}
/**
* 函数功能:重写sharepreference初始化函数
*
* @param
*
* @return null
*
*/
@Override
protected void onSetInitialValue(boolean restore, Object defaultValue)
{
super.onSetInitialValue(restore, defaultValue);
if (restore) {
mValue = shouldPersist() ? getPersistedInt(mDefault) : 0;
Log.e("SeekBarPreference", "onSetInitialValue--true--mValue:"+mValue+"mDefault:"+mDefault);
}else{
mValue = (Integer)defaultValue;
Log.e("SeekBarPreference", "onSetInitialValue--false--mValue:"+mValue);
}
}
/**
* 函数功能:重写seekbar中progress值改变函数
*
* @param
*
* @return null
*
*/
public void onProgressChanged(SeekBar seek, int value, boolean fromTouch)
{
String volum=String.valueOf(100*value/getMax())+"%";
mValueText.setText(mSuffix == null ? volum : mSuffix.concat(volum));
if (shouldPersist()){
persistInt(value);
Log.d("SeekBarPreference", "onProgressChanged---onProgressChanged:"+persistInt(value));
}
callChangeListener(new Integer(value));
this.mValue=value;
Log.d("SeekBarPreference", "onProgressChanged---onProgressChanged:"+value);
}
/**
* 函数功能:重写开始滑动seekbar
*
* @param null
*
* @return null
*
*/
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
Log.e("SeekBarPreference", "onStartTrackingTouch---oldValue:"+oldValue);
}
/**
* 函数功能:重写停止滑动seekbar
*
* @param null
*
* @return null
*
*/
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
updatePreference(mValue);
notifyChanged();
Log.e("SeekBarPreference", "onStopTrackingTouch+preValue:"+getPreferenceValue()+"mvalue:"+mValue);
}
/**
* 函数功能:dialog关闭
*
* @param
*
* @return null
*
*/
@Override
protected void onDialogClosed(boolean positiveResult) {
// TODO Auto-generated method stub
if (positiveResult) {
updatePreference(mValue);
notifyChanged();
Log.e("SeekBarPreference", "onDialogClosed--positive button---mValue:"+mValue);
} else {
updatePreference(oldValue);
notifyChanged();
Log.e("SeekBarPreference", "onDialogClosed--negative button---oldValue:"+oldValue);
}
}
/**
* 函数功能:设置seekbar中的max
*
* @param
*
* @return null
*
*/
public void setMax(int max) {
mMax = max;
if (mSeekBar != null)
mSeekBar.setMax(mMax);
}
/**
* 函数功能:得到seekbar中的max
*
* @param null
*
* @return mMax
*
*/
public int getMax() {
return mMax;
}
/**
* 函数功能:设置seekbar中的progress
*
* @param
*
* @return null
*
*/
public void setProgress(int progress) {
mValue = progress;
if (mSeekBar != null)
mSeekBar.setProgress(progress);
}
/**
* 函数功能:得到seekbar中的progress
*
* @param null
*
* @return mValue
*
*/
public int getProgress() {
return mValue;
}
/**
* 函数功能:更新sharepreference中的值
*
* @param
*
* @return null
*
*/
private void updatePreference(int newValue) {
SharedPreferences.Editor editor = getEditor();
editor.putInt(getKey(), newValue);
editor.commit(); //更新 SharedPreferences 配置文件中的值
}
/**
* 函数功能:得到sharepreference中的值
*
* @param null
*
* @return null
*
*/
public int getPreferenceValue() {
if (shouldPersist()){
this.preValue= getPersistedInt(mDefault);
}
Log.e("SeekBarPreference", "getPreferenceValue---preValue:"+preValue);
return this.preValue;
}
/**
* 函数功能:得到打开dialog之前sharepreference中的值
*
* @param null
*
* @return null
*
*/
public void getOldValue() {
this.oldValue= shouldPersist() ? getPersistedInt(mDefault) : 0;
Log.e("SeekBarPreference", "getOldValue---oldValue:"+oldValue);
}
}
2、使用:
A、在preference中直接配置:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:layout="@layout/preference_category" >
<com.cindigo.MyView.SeekBarPreference
android:dialogTitle="@string/preference_volume_dialogtitle"
android:key="preference_volume"
android:title="@string/preference_volume_title"
android:summary="@string/preference_volume_summary"
android:defaultValue="50"
android:max="100"
android:text="音量:"
android:dialogMessage="请在此调节游戏音量大小:"
android:layout="@layout/preference_layout_seekbar"
android:widgetLayout="@layout/preference_arrow"/>
</PreferenceScreen>
B、代码中监听事件:
public class SettingActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
private SeekBarPreference sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.buttonbar);
addPreferencesFromResource(R.xml.custom_setting);
sp= PreferenceManager.getDefaultSharedPreferences(this);
sp.registerOnSharedPreferenceChangeListener(this);//监听数据变化,调节屏幕亮度
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sp, String key) {
// TODO Auto-generated method stub
int brightness=sp.getInt("brightness", 125);
LayoutParams lp= getWindow().getAttributes();
lp.screenBrightness=brightness/255.0f;
getWindow().setAttributes(lp);
}
}
二、全局亮度(修改手机系统亮度)
(1)说明:
Settings.
System.putInt(getContentResolver(), Settings.
System.SCREEN_BRIGHTNESS, bright);
可以改变全局亮度,但要让它生效可能需要,将模式改为手动
Settings.
System.putInt(getContentResolver(),Settings.
System.SCREEN_BRIGHTNESS_MODE, Settings.
System.SCREEN_BRIGHTNESS_MODE_MANUAL);
要调用这些API,需要权限:
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
(2)代码说明:
Android的屏幕亮度好像在2.1+的时候提供了自动调节的功能,所以,如果当开启自动调节功能的时候, 我们进行调节好像是没有一点作用的,这点让我很是无语,结果只有进行判断,看是否开启了屏幕亮度的自动调节功能。
/**
* 判断是否开启了自动亮度调节
*
* @param aContext
* @return
*/
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;
}
然后就是要觉得当前的亮度了,这个就比较纠结了:
/**
* 获取屏幕的亮度
*
* @param activity
* @return
*/
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;
}
那如何修改屏幕的亮度呢?
/**
* 设置亮度
*
* @param activity
* @param brightness
*/
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);
activity.getWindow().setAttributes(lp);
}
那么,能设置了,但是为什么还是会出现,设置了,没反映呢?
嘿嘿,那是因为,开启了自动调节功能了,那如何关闭呢?这才是最重要的:
/**
* 停止自动亮度调节
*
* @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);
}
至此,应该说操作亮度的差不多都有了,结束!
哎,本来认为是应该结束了,但是悲剧得是,既然像刚才那样设置的话,只能在当前的activity中有作用,一段退出的时候,会发现毫无作用,悲剧,原来是忘记了保存了。
/**
* 保存亮度设置状态
*
* @param resolver
* @param brightness
*/
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);
}
这回该差不多了。
(3)封装:
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.Window;
import android.view.WindowManager;
public class ScreenBrightnessTool
{
/**
* Activty自动调节亮度模式
*/
public static final int ACTIVITY_BRIGHTNESS_AUTOMATIC = -1;
/**
* 自动调节模式
*/
public static final int SCREEN_BRIGHTNESS_MODE_AUTOMATIC = Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
/**
* 手动调节模式
*/
public static final int SCREEN_BRIGHTNESS_MODE_MANUAL = Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
/**
* 默认亮度
*/
public static final int SCREEN_BRIGHTNESS_DEFAULT = 75;
/**
* 最大亮度
*/
public static final int MAX_BRIGHTNESS = 100;
/**
* 最小亮度
*/
public static final int MIN_BRIGHTNESS = 0;
private static final int mMaxBrighrness = 255;
private static final int mMinBrighrness = 120;
// 当前系统调节模式
private boolean sysAutomaticMode;
// 当前系统亮度值
private int sysBrightness;
private Context context;
private ScreenBrightnessTool(Context context, int sysBrightness,
boolean sysAutomaticMode)
{
this.context = context;
this.sysBrightness = sysBrightness;
this.sysAutomaticMode = sysAutomaticMode;
}
/**
* 创建屏幕亮度工具
*
* @param context
* @return
*/
public static ScreenBrightnessTool Builder(Context context)
{
int brightness;
boolean automaticMode;
try
{
// 获取当前系统亮度值
brightness = Settings.System.getInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS);
// 获取当前系统调节模式
automaticMode = Settings.System.getInt(
context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE) == SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
}
catch (SettingNotFoundException e)
{
return null;
}
return new ScreenBrightnessTool(context, brightness, automaticMode);
}
/**
* 返回当前系统亮度调节模式
*
* @return
*/
public boolean getSystemAutomaticMode()
{
return sysAutomaticMode;
}
/**
* 返回当前系统亮度值
*
* @return
*/
public int getSystemBrightness()
{
return sysBrightness;
}
/**
* 设置调节模式
*
* @param mode
* 调节模式
*/
public void setMode(int mode)
{
if (mode != SCREEN_BRIGHTNESS_MODE_AUTOMATIC
&& mode != SCREEN_BRIGHTNESS_MODE_MANUAL)
return;
sysAutomaticMode = mode == SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
Settings.System.putInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE, mode);
}
/**
* 设置屏幕亮度
*
* @param brightness
* 亮度值,值为0至100
*/
public void setBrightness(int brightness)
{
int mid = mMaxBrighrness - mMinBrighrness;
int bri = (int) (mMinBrighrness + mid * ((float) brightness)
/ MAX_BRIGHTNESS);
ContentResolver resolver = context.getContentResolver();
Settings.System
.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, bri);
}
/**
* 亮度预览
*
* @param activity
* 预览activity
* @param brightness
* 亮度值(0.47~1)
*/
public static void brightnessPreview(Activity activity, float brightness)
{
Window window = activity.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.screenBrightness = brightness;
window.setAttributes(lp);
}
/**
* 亮度预览
*
* @param activity
* 预览activity
* @param percent
* 百分比(0.0~1.00)
*/
public static void brightnessPreviewFromPercent(Activity activity,
float percent)
{
float brightness = percent + (1.0f - percent)
* (((float) mMinBrighrness) / mMaxBrighrness);
brightnessPreview(activity, brightness);
}
}
三:更多参考:
android 关于调节屏幕亮度的问题-CSDN问答Source URL:http://ask.csdn.net/questions/170 |
Android 调节屏幕亮度问题 - kerlubasola - ITeye技术网站Source URL:http://kerlubasola.iteye.com/blog/1581385 |
Android调节屏幕亮度 - android 开发专栏 - 博客频道 - CSDN.NETSource URL:http://blog.csdn.net/androidchuxueze/article/details/7452438 |
Android-Android屏幕亮度设置 - 德问:编程社交问答Source URL:http://www.dewen.org/q/2519 |