Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android開發之調節屏幕亮度

Android開發之調節屏幕亮度

編輯:關於Android編程

在播放器中,我們常常可以看到這麼一個設計,就是用戶通過在屏幕的某個部分上下滑動就可以調節屏幕的亮度,在某個部分上下滑動就可以調節播放的音量。而左右滑動就可以調節播放的進度。

今天,我要說一下亮度的調節。其實主要是通過設置View的屬性實現的。

  public void onLightChange(float delta, int distance, Window window) {
        WindowManager.LayoutParams params = window.getAttributes();

        mCurrentDeltaBrintness = mCurrentDeltaBrintness + delta;

        if (params.screenBrightness == WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE) {
            params.screenBrightness = getScreenBrightness(getContext());
        }

        if (Math.abs(mCurrentDeltaBrintness) >= mScrolledPixPerBringhtness) {
            float deltaBrightness = mCurrentDeltaBrintness / (mScrolledPixPerBringhtness * MAX_BRINTNESS);
            params.screenBrightness = params.screenBrightness + deltaBrightness;
    private void performLightChange(float brightness) {
        int resId = R.drawable.light_0;

        if (brightness <= 0.01f) {
            resId = R.drawable.light_0;
        } else if (brightness <= 0.25f) {
            resId = R.drawable.light_25;
        } else if (brightness <= 0.5f) {
            resId = R.drawable.light_50;
        } else if (brightness < 1.0f) {
            resId = R.drawable.light_75;
        } else {
            resId = R.drawable.light_100;
        }

        updateViews(resId, (int) (brightness * 100));
    }

if (params.screenBrightness <= 0.01f) { params.screenBrightness = 0.01f; } window.setAttributes(params); mCurrentDeltaBrintness = 0; } performLightChange(params.screenBrightness); }


上述代碼就是在左邊部分上下滑動的時候會被調用。

另附performLigthChange的代碼。

    private void performLightChange(float brightness) {
        int resId = R.drawable.light_0;

        if (brightness <= 0.01f) {
            resId = R.drawable.light_0;
        } else if (brightness <= 0.25f) {
            resId = R.drawable.light_25;
        } else if (brightness <= 0.5f) {
            resId = R.drawable.light_50;
        } else if (brightness < 1.0f) {
            resId = R.drawable.light_75;
        } else {
            resId = R.drawable.light_100;
        }

        updateViews(resId, (int) (brightness * 100));
    }

我們在舉一個相對好切入的例子。

復制代碼
 1 package android.lekko.tools;
 2 
 3 import android.app.Activity;
 4 import android.content.ContentResolver;
 5 import android.provider.Settings;
 6 import android.provider.Settings.System;
 7 import android.view.WindowManager;
 8 import android.widget.Toast;
 9 
10 public class LightnessControl {
11     // 判斷是否開啟了自動亮度調節 
12     public static boolean isAutoBrightness(Activity act) { 
13         boolean automicBrightness = false; 
14         ContentResolver aContentResolver = act.getContentResolver();
15         try { 
16             automicBrightness = Settings.System.getInt(aContentResolver, 
17                    Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; 
18         } catch (Exception e) { 
19             Toast.makeText(act,"無法獲取亮度",Toast.LENGTH_SHORT).show();
20         } 
21         return automicBrightness; 
22     }     
23     // 改變亮度
24     public static void SetLightness(Activity act,int value)
25     {        
26         try {
27             System.putInt(act.getContentResolver(),System.SCREEN_BRIGHTNESS,value); 
28             WindowManager.LayoutParams lp = act.getWindow().getAttributes(); 
29             lp.screenBrightness = (value<=0?1:value) / 255f;
30             act.getWindow().setAttributes(lp);
31         } catch (Exception e) {
32             Toast.makeText(act,"無法改變亮度",Toast.LENGTH_SHORT).show();
33         }        
34     }
35     // 獲取亮度
36     public static int GetLightness(Activity act)
37     {
38         return System.getInt(act.getContentResolver(),System.SCREEN_BRIGHTNESS,-1);
39     }
40     // 停止自動亮度調節 
41     public static void stopAutoBrightness(Activity activity) { 
42         Settings.System.putInt(activity.getContentResolver(), 
43                 Settings.System.SCREEN_BRIGHTNESS_MODE, 
44                 Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); 
45     }
46     // 開啟亮度自動調節 
47     public static void startAutoBrightness(Activity activity) { 
48         Settings.System.putInt(activity.getContentResolver(), 
49                 Settings.System.SCREEN_BRIGHTNESS_MODE, 
50                 Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC); 
51     } 
52 }
復制代碼
  這是一個單獨可工作的類,主要用於調節屏幕亮度,有部分注釋了,解釋幾個概念,不標准,供參考:
  ContentResolver類,為訪問其它應用程序的對外共享數據提供方法,如上面獲取、設置亮度時使用的System.getInt(),System.setInt()。
  Activity類,android程序的主要類,一個界面必有此類提供後台支持,需要繼承此類。
  Settings類,android程序系統相關的設置類,各種設置都可在此找。
  LayoutParams類,android界面相關的參數,如高度、寬度、亮度等。
  Toast類,一個可自動消失的提示框,輕型控件。


好,屏幕的亮度調節就到這裡,很簡單的。






  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved