在製作專題系統的時候,碰巧需要模擬燈光操控系統,由於沒有真正的燈光系統,靈光一閃地,想到用android平板的背景亮度來模擬現實,於是就實作了這隻小程式來「模擬」!
範例描述:
小程式包含一條可以平滑操控背光亮暗的"拖曳條"(SeekBar),與四個可以直接設定亮度的"按鈕"(Button)。而核心控制方法則是setBrightness(int newBright),為將四個按鈕重複的方法獨立出來所重構的方法。
1.MainActivity.java
// RoastFish 2013/11/26
package com.example.brightnessexample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends Activity {
float BackLightValue = 0.5f; //default value
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
setListeners();
}
SeekBar BackLightControl ;
TextView BackLightSetting ;
Button UpdateSystemSetting ;
private Button[] test = new Button[4];
// defined the layout elements
private void findViews() {
BackLightControl = (SeekBar) findViewById(R.id.backlightcontrol);
BackLightSetting = (TextView) findViewById(R.id.backlightsetting);
UpdateSystemSetting = (Button) findViewById(R.id.updatesystemsetting);
test[0] = (Button) findViewById(R.id.test01);
test[1] = (Button) findViewById(R.id.test02);
test[2] = (Button) findViewById(R.id.test03);
test[3] = (Button) findViewById(R.id.test04);
}
// Listen for button clicks
private void setListeners() {
BackLightControl.setOnSeekBarChangeListener(LightBar);
UpdateSystemSetting.setOnClickListener(update);
test[0].setOnClickListener(brightness10);
test[1].setOnClickListener(brightness30);
test[2].setOnClickListener(brightness70);
test[3].setOnClickListener(brightness100);
}
//10 %
private Button.OnClickListener brightness10 = new Button.OnClickListener() {
public void onClick(View v) {
setBrightness(10);
BackLightSetting.setText(String.valueOf(0.1));
}
};
//30 %
private Button.OnClickListener brightness30 = new Button.OnClickListener() {
public void onClick(View v) {
setBrightness(30);
BackLightSetting.setText(String.valueOf(0.3));
}
};
//70 %
private Button.OnClickListener brightness70 = new Button.OnClickListener() {
public void onClick(View v) {
setBrightness(70);
BackLightSetting.setText(String.valueOf(0.7));
}
};
//100 %
private Button.OnClickListener brightness100 = new Button.OnClickListener() {
public void onClick(View v) {
setBrightness(100);
BackLightSetting.setText(String.valueOf(1.0));
}
};
//Light Bar
private SeekBar.OnSeekBarChangeListener LightBar = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
BackLightValue = (float) arg1 / 100;
BackLightSetting.setText(String.valueOf(BackLightValue));
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = BackLightValue;
getWindow().setAttributes(layoutParams);
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
}
@Override
public void onStopTrackingTouch(SeekBar arg0) {
}
};
//Update
private Button.OnClickListener update = new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
int SysBackLightValue = (int) (BackLightValue * 255);
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
SysBackLightValue);
}
};
/**
* Set the program's screen brightness.
* @param newBright The brightness yout want to set.
*/
public void setBrightness(int newBright){
int brightness=50;
//Brightness Range : 0<newBright<=100
//0 : Screen will shut down.
if(newBright <1){
brightness = 1;
}else if(newBright >100){
brightness = 100;
}else{
brightness = newBright;
}
BackLightValue = (float) brightness / 100;
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = BackLightValue;
getWindow().setAttributes(layoutParams);
}
}
2.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set BackLight of the App" />
<SeekBar
android:id="@+id/backlightcontrol"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10px"
android:max="100"
android:progress="50" />
<TextView
android:id="@+id/backlightsetting"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="0.50" />
<Button
android:id="@+id/updatesystemsetting"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Update Settings.System.SCREEN_BRIGHTNESS" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/test01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10%" />
<Button
android:id="@+id/test02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="40%" />
<Button
android:id="@+id/test03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="70%" />
<Button
android:id="@+id/test04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100%" />
</LinearLayout>
</LinearLayout>
3.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.brightnessexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.brightnessexample.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

沒有留言:
張貼留言