电脑屏幕亮度调节器器超暗not 2手机

使用合作网站登录HIKe:
我的相机,HIKe智能手机
Copyright (C) 2012
Co.,Ltd. All Rights Reserved.粤ICP备号android调节屏幕亮度(包括只修改应用程序和修改系统)
android调节屏幕亮度(包括只修改应用程序和修改系统)
本帖最后由 jairkong 于
01:26 编辑
一:只改变当前程序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,然后设置。
当离开当前Acitivity时,屏幕亮度会恢复到原先的亮度。另外将lp.screenBrightness 设为 -1(),也会让屏幕恢复到原先的亮度(即系统设置)。
(3)最小亮度
,官方文档说这个值可以将屏幕亮度设置到最低亮度(Lowest
Brightness)。实际意识是将屏幕设置到全黑,屏幕也无法响应触控了。
在G3(CM6)上,将屏幕设置到最低亮度值是0.004(精度0.001),这时屏幕基本全黑,但仍能控制。低于0.004(精度0.001)时,屏幕便失去控制。0.01也是个要记录的值,屏幕亮度足够低,当仍能看到东西。这些值应该是和设备有关的,不能乱设。
(4)封装进度条控制
1、封装类:
com.cindigo.MyV
android.content.C
import android.content.SharedP
import android.graphics.C
import android.preference.DialogP
import android.preference.PreferenceM
import android.util.AttributeS
import android.util.L
import android.view.G
import android.view.V
import android.widget.LinearL
import android.widget.SeekB
import android.widget.TextV
public class SeekBarPreference extends DialogPreference implements
SeekBar.OnSeekBarChangeListener
private static final String
androidns="/apk/res/android";
public SeekBar
private TextView mSplashText,mValueT
private Context mC
private String
mDialogMessage, mS
private int mDefault, mMax, mValue =
0,oldValue=0,preValue=0;
public SeekBarPreference(Context context, AttributeSet attrs)
super(context,attrs);&
mContext =
mDialogMessage =
attrs.getAttributeValue(androidns,"dialogMessage");
mSuffix = attrs.getAttributeValue(androidns,"text");
mDefault = attrs.getAttributeIntValue(androidns,"defaultValue",
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");&
@Override&
protected View onCreateDialogView() {
LinearLayout.LayoutP
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);
@Override&
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
//设置progressbar属性
setMax(mMax);
setProgress(mValue);
Log.e("SeekBarPreference",
"onBindDialogView---mValue:"+mValue);&
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);&
mValue = (Integer)defaultV
Log.e("SeekBarPreference",
"onSetInitialValue--false--mValue:"+mValue);&
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=
Log.d("SeekBarPreference",
"onProgressChanged---onProgressChanged:"+value);
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
Log.e("SeekBarPreference",
"onStartTrackingTouch---oldValue:"+oldValue);
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
updatePreference(mValue);
notifyChanged();
Log.e("SeekBarPreference",
"onStopTrackingTouch+preValue:"+getPreferenceValue()+"mvalue:"+mValue);
@Override&
protected void onDialogClosed(boolean positiveResult)
// TODO Auto-generated method stub&
if (positiveResult) {
updatePreference(mValue);
notifyChanged();
Log.e("SeekBarPreference", "onDialogClosed--positive
button---mValue:"+mValue);&
updatePreference(oldValue);
notifyChanged();
Log.e("SeekBarPreference", "onDialogClosed--negative
button---oldValue:"+oldValue);&
public void setMax(int max) {&
if (mSeekBar != null)
mSeekBar.setMax(mMax);
public int getMax() {&
return mM&
public void setProgress(int progress) {&
if (mSeekBar != null)
mSeekBar.setProgress(progress);
public int getProgress() {&
return mV&
private void updatePreference(int newValue)
SharedPreferences.Editor editor =
getEditor();&
editor.putInt(getKey(), newValue);&
mit(); //更新 SharedPreferences
配置文件中的值&
public int getPreferenceValue() {&
if (shouldPersist()){
this.preValue= getPersistedInt(mDefault);
Log.e("SeekBarPreference",
"getPreferenceValue---preValue:"+preValue);&
return this.preV
public void getOldValue() {&
this.oldValue= shouldPersist() ? getPersistedInt(mDefault) :
Log.e("SeekBarPreference",
"getOldValue---oldValue:"+oldValue);&
A、在preference中直接配置:
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"/&
B、代码中监听事件:
public class
SettingActivity extends PreferenceActivity implements
OnSharedPreferenceChangeListener {
&private&&SeekBarPreference
& & @Override
& & protected void
onCreate(Bundle savedInstanceState) {
&&&// TODO
Auto-generated method stub
&&&super.onCreate(savedInstanceState);
&&&setContentView(R.layout.buttonbar);
&&&addPreferencesFromResource(R.xml.custom_setting);
PreferenceManager.getDefaultSharedPreferences(this);
sp.registerOnSharedPreferenceChangeListener(this);//监听数据变化,调节屏幕亮度
& & @Override
& & public void
onSharedPreferenceChanged(SharedPreferences sp, String key)
&&&// TODO
Auto-generated method stub
brightness=sp.getInt("brightness", 125);
&&&LayoutParams
&getWindow().getAttributes();
lp.screenBrightness=brightness/255.0f;
getWindow().setAttributes(lp);
二、全局亮度(修改手机系统亮度)
(1)说明:
&&&Settings..putInt(getContentResolver(),&
&&&Settings..SCREEN_BRIGHTNESS,
可以改变全局亮度,但要让它生效可能需要,将模式改为手动
&&&Settings..putInt(getContentResolver(),Settings..SCREEN_BRIGHTNESS_MODE,&
Settings..SCREEN_BRIGHTNESS_MODE_MANUAL);
要调用这些API,需要权限:
(2)代码说明:
Android的屏幕亮度好像在2.1+的时候提供了自动调节的功能,所以,如果当开启自动调节功能的时候,
我们进行调节好像是没有一点作用的,这点让我很是无语,结果只有进行判断,看是否开启了屏幕亮度的自动调节功能。
& & public static boolean
isAutoBrightness(ContentResolver aContentResolver) {
&&&boolean
automicBrightness =
&automicBrightness =
Settings.System.getInt(aContentResolver,
&&&Settings.System.SCREEN_BRIGHTNESS_MODE)
== Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
&&&} catch
(SettingNotFoundException e) {
&e.printStackTrace();
然后就是要觉得当前的亮度了,这个就比较纠结了:
& & public static int
getScreenBrightness(Activity activity) {
nowBrightnessValue = 0;
&&&ContentResolver
resolver = activity.getContentResolver();
&nowBrightnessValue =
android.provider.Settings.System.getInt(
&&&resolver,
Settings.System.SCREEN_BRIGHTNESS);
&&&} catch
(Exception e) {
&e.printStackTrace();
nowBrightnessV
那如何修改屏幕的亮度呢?
& & 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);
那么,能设置了,但是为什么还是会出现,设置了,没反映呢?
嘿嘿,那是因为,开启了自动调节功能了,那如何关闭呢?这才是最重要的:
& & 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);
至此,应该说操作亮度的差不多都有了,结束!
哎,本来认为是应该结束了,但是悲剧得是,既然像刚才那样设置的话,只能在当前的activity中有作用,一段退出的时候,会发现毫无作用,悲剧,原来是忘记了保存了。
& & 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,
这回该差不多了。
(3)封装:
import android.app.A
import android.content.ContentR
import android.content.C
import android.provider.S
android.provider.Settings.SettingNotFoundException;
import android.view.W
import android.view.WindowM
public class ScreenBrightnessTool
& & 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
sysAutomaticM
& & // 当前系统亮度值
& & private int
& & private Context
& & private
ScreenBrightnessTool(Context context, int sysBrightness,
&& &boolean
sysAutomaticMode)
&&&this.context
&&&this.sysBrightness
&&&this.sysAutomaticMode
= sysAutomaticM
& & public static
ScreenBrightnessTool Builder(Context context)
&&&boolean
automaticM
获取当前系统亮度值
&& &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;
(SettingNotFoundException e)
&& &return
&&&return new
ScreenBrightnessTool(context, brightness, automaticMode);
& & public boolean
getSystemAutomaticMode()
sysAutomaticM
& & public int
getSystemBrightness()
& & public void setMode(int
&&&if (mode !=
SCREEN_BRIGHTNESS_MODE_AUTOMATIC
mode != SCREEN_BRIGHTNESS_MODE_MANUAL)
&&&sysAutomaticMode
= mode == SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
&&&Settings.System.putInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE, mode);
& & public void setBrightness(int
brightness)
&&&int mid =
mMaxBrighrness - mMinB
&&&int bri =
(int) (mMinBrighrness + mid * ((float) brightness)
MAX_BRIGHTNESS);
&&&ContentResolver
resolver = context.getContentResolver();
&&&Settings.System
.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS,
& & public static void
brightnessPreview(Activity activity, float brightness)
&&&Window window
= activity.getWindow();
&&&WindowManager.LayoutParams
lp = window.getAttributes();
&&&lp.screenBrightness
&&&window.setAttributes(lp);
& & public static void
brightnessPreviewFromPercent(Activity activity,
brightness = percent + (1.0f - percent)
&& & * (((float)
mMinBrighrness) / mMaxBrighrness);
&&&brightnessPreview(activity,
brightness);
三:更多参考:
关于调节屏幕亮度的问题-CSDN问答Source
调节屏幕亮度问题 - kerlubasola - ITeye技术网站Source
Android调节屏幕亮度 - android
开发专栏 - 博客频道 - CSDN.NETSource
Android-Android屏幕亮度设置 -
德问:编程社交问答Source
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

我要回帖

更多关于 屏幕亮度调节器超暗 的文章

 

随机推荐