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;
<pre name="code" class="java">    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类。自己主动的消息框消失,光控。


大,调整屏幕亮度到这里,非常easy的。






posted @ 2015-12-09 09:32  yxwkaifa  阅读(1485)  评论(0编辑  收藏  举报