android 改变app亮度不影响系统亮度(获取系统亮度)
来源:互联网 发布:单片机流水灯实验报告 编辑:程序博客网 时间:2024/04/30 23:43
在做改变亮度的时候基本都是改变系统的亮度,那如何改变app亮度又不影响系统亮度呢?在app上改变亮度后,退出app亮度恢复成系统原先的亮度,但是在次进入app的时候,亮度还要恢复成app当初设置的亮度。
需要demo的拉倒底部可以下载。
代码:
布局文件
activity_main.xml
<RelativeLayout 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" > <SeekBar android:id="@+id/seekBar1" android:layout_width="match_parent" android:layout_height="wrap_content" style="@style/Widget.SeekBar.Normal" android:layout_marginTop="124dp" /></RelativeLayout>
values文件夹下的style文件
style.xml
<style name="Widget.SeekBar.Normal" parent="@android:style/Widget.SeekBar"> <item name="android:maxHeight">8.0dip</item> <item name="android:indeterminateOnly">false</item> <item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item> <item name="android:progressDrawable">@drawable/seekbar_horizontal</item> <item name="android:minHeight">8.0dip</item> <item name="android:thumb">@drawable/seek_thumb</item> <item name="android:thumbOffset">10.0dip</item> </style>
在res文件夹下创建一个drawable文件夹,里面放:
seekbar_horizontal.xml
<?xml version="1.0" encoding="UTF-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background" android:drawable="@drawable/seek_bkg" /> <item android:id="@android:id/secondaryProgress"> <clip> <shape> <corners android:radius="2.0dip" /> <gradient android:startColor="#80ffd300" android:endColor="#a0ffcb00" android:angle="270.0" android:centerY="0.75" android:centerColor="#80ffb600" /> </shape> </clip> </item> <item android:id="@android:id/progress"> <clip android:drawable="@drawable/seek" /> </item></layer-list>
主activity
MainActivity.java
package com.example.liangdu;import android.app.Activity;import android.content.ContentResolver;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.os.Bundle;import android.provider.Settings;import android.provider.Settings.SettingNotFoundException;import android.view.Window;import android.view.WindowManager;import android.widget.SeekBar;public class MainActivity extends Activity { private SeekBar seekBar; private SharedPreferences shared = null; private int num=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); seekBar = (SeekBar) findViewById(R.id.seekBar1); shared=getSharedPreferences("base64",MODE_PRIVATE); num=shared.getInt("seekBarNum", 0); changeAppBrightness(num); seekBar.setProgress(num); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub /* 结束 */ } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub /* 开始 */ } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub changeAppBrightness(seekBar.getProgress()); } }); } // 获取系统屏幕亮度 public int getScreenBrightness() { int value = 0; ContentResolver cr = getContentResolver(); try { value = Settings.System.getInt(cr, Settings.System.SCREEN_BRIGHTNESS); } catch (SettingNotFoundException e) { } return value; } // 获取app亮度 public void changeAppBrightness(int brightness) { Window window = getWindow(); WindowManager.LayoutParams lp = window.getAttributes(); lp.screenBrightness = (brightness <= 0 ? 1 : brightness) / 255f; window.setAttributes(lp); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); Editor editor= shared.edit(); editor.clear(); editor.putInt("seekBarNum", seekBar.getProgress()); editor.commit(); }}
Editor 保存操作我是放在了生命周期onStop()方法里,因为每次退出都要保存当前seekBar的值。
源码demo:http://download.csdn.net/detail/qq_17299423/9527147
0 0
- android 改变app亮度不影响系统亮度(获取系统亮度)
- Android 调节系统亮度
- Android 设置系统亮度
- android 系统亮度设置
- Android设置系统亮度
- XP系统改变显示亮度
- android系统屏幕亮度调节
- android 设置系统屏幕亮度
- android 设置系统屏幕亮度
- android 设置系统屏幕亮度
- android 设置系统屏幕亮度
- android 设置系统屏幕亮度
- android 设置系统屏幕亮度
- android 设置系统屏幕亮度
- android 设置系统屏幕亮度
- android 设置系统屏幕亮度
- android 设置系统屏幕亮度
- android 设置系统屏幕亮度
- [bzoj4544]椭圆上的整点 解题报告
- Java四种线程池的使用以及callable future整理
- Servlet技术
- easyui扩展 - 验证两个字符串是否相同
- 函数模板和类模板
- android 改变app亮度不影响系统亮度(获取系统亮度)
- Android ORM 框架——greenDAO
- Linux 调优方案
- 最大连续子序列和
- Jmeter基础(一)---元件作用域与执行顺序
- PHP添加新模块
- 安卓登录界面让输入框滚动到指定位置的方法
- 文章标题
- nodejs meanjs meanio yo等