在播放器中,我们常常可以看到这么一个设计,就是用户通过在屏幕的某个部分上下滑动就可以调节屏幕的亮度,在某个部分上下滑动就可以调节播放的音量。而左右滑动就可以调节播放的进度。
今天,我要说一下亮度的调节。其实主要是通过设置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类,一个可自动消失的提示框,轻型控件。