一:只改变当前程序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);
(2)恢复
(3)最小亮度
(4)封装进度条控制
1、封装类:
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;
* 自定义SeekBarPreference
*/
public class SeekBarPreference extends DialogPreference implements SeekBar.OnSeekBarChangeListener
{
private static final String androidns="http://schemas.android.com/apk/res/android";
private TextView mSplashText,mValueText;
private Context mContext;
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.setTextColor(Color.WHITE);
mSplashText.setTextSize(20);
if (mDialogMessage != null)
mSplashText.setText(mDialogMessage);
layout.addView(mSplashText);
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);
}
}
<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"/>
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);
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
(2)代码说明:
* 判断是否开启了自动亮度调节
*
* @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);
}
* 保存亮度设置状态
*
* @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);
}
}
留言列表