แสดงบทความที่มีป้ายกำกับ ANDROID แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ ANDROID แสดงบทความทั้งหมด
เนื่องจากอยู่กับ IOIO มานานแล้วเลยจัดโครงการ IOIO BOT ขึ้นเป็นโครงการสอนการเขียน Application Android พื้นฐาน และ Android for control IOIO board


วันนี้เป็นบทความ screen brightness ปกติเวลาจะปรับค่าความสว่างของหน้าจอ Android จะต้องทำผ่านการตั้งค่า แต่คราวนี้ จะมาลองเขียน Application เพื่อปรับค่าความสว่างของหน้าจอเองซะเลย

ก่อนอื่นเลยต้อง Create project ขึ้นมาก่อน 

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" tools:context="${relativePackage}.${activityClass}" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="109dp" android:layout_marginTop="50dp" android:text="Brightness Screen" /> <SeekBar android:id="@+id/seekBar1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView1" android:layout_marginTop="100dp" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/seekBar1" android:layout_alignLeft="@+id/textView1" android:layout_marginBottom="48dp" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" />
MainActivity.java
package com.NUrobot.brightnessscreen; import android.app.Activity; import android.content.ContentResolver; import android.os.Bundle; import android.provider.Settings.SettingNotFoundException; import android.provider.Settings.System; import android.util.Log; import android.view.Window; import android.view.WindowManager.LayoutParams; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; public class MainActivity extends Activity { private SeekBar brightbar; //Variable to store brightness value private int brightness; //Content resolver used as a handle to the system's settings private ContentResolver cResolver; //Window object, that will store a reference to the current window private Window window; private TextView txtPerc; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); brightbar = (SeekBar) findViewById(R.id.seekBar1); txtPerc = (TextView) findViewById(R.id.textView2); //Get the content resolver cResolver = getContentResolver(); //Get the current window window = getWindow(); //Set the seekbar range between 0 and 255 brightbar.setMax(255); //Set the seek bar progress to 1 brightbar.setKeyProgressIncrement(1);try { //Get the current system brightness brightness = System.getInt(cResolver,System.SCREEN_BRIGHTNESS); } catch (SettingNotFoundException e) { //Throw an error case it couldn't be retrieved Log.e("Error", "Cannot access system brightness"); e.printStackTrace(); } //Set the progress of the seek bar based on the system's brightness brightbar.setProgress(brightness); //Register OnSeekBarChangeListener, so it can actually change values brightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { public void onStopTrackingTouch(SeekBar seekBar) { //Set the system brightness using the brightness variable value System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness); //Get the current window attributes LayoutParams layoutpars = window.getAttributes(); //Set the brightness of this window layoutpars.screenBrightness = brightness / (float)255; //Apply attribute changes to this window window.setAttributes(layoutpars); } public void onStartTrackingTouch(SeekBar seekBar) { //Nothing handled here } public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { //Set the minimal brightness level //if seek bar is 20 or any value below if(progress<=20) { //Set the brightness to 20 brightness=20; } else //brightness is greater than 20 { //Set brightness variable based on the progress bar brightness = progress; } //Calculate the brightness percentage float perc = (brightness /(float)255)*100; //Set the brightness percentage txtPerc.setText((int)perc +" %"); } }); }}
AndroidMannifest.xml เพิ่ม permission 

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
คลิกขวา >> project >> Run as >> Android application

เรียบร้อย...