• 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的。






  • 相关阅读:
    floyd的魔改应用——洛谷P2419 [USACO08JAN]牛大赛Cow Contest 题解
    洛谷P2142 高精度减法 题解
    浅谈SPFA——洛谷P1576 最小花费 题解
    洛谷P1301 魔鬼之城 题解
    洛谷P1009 阶乘之和 题解
    20200926模拟
    [NOIP 2013]货车运输
    带权并查集--P2024 [NOI2001]食物链
    归并排序/树状数组求逆序对-lgP1908 逆序对
    LCA模块+求树上两点距离最短
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5031813.html
Copyright © 2020-2023  润新知