android 改變app亮度不影響系統亮度(獲取系統亮度)
阿新 • • 發佈:2019-01-28
在做改變亮度的時候基本都是改變系統的亮度,那如何改變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();
}
}