In the player, we can often see a design where the user can adjust the brightness of the screen by swiping up and down on one part of the screen, and swipe up or down in a section to adjust the volume of the play. and left and right swipe to adjust the playback progress.
Today, I would like to say a bit about the brightness adjustment. In fact, mainly by setting the properties of the 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 = MCurr Entdeltabrintness/(Mscrolledpixperbringhtness * max_brintness); params.screenbrightness = params.screenbrightness + deltabrightness;<pre name= "code" class= "java" > private void P Erformlightchange (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); }
The above code is called when the left part slides up and down.
A separate code for Performligthchange is attached.
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.drawa ble.light_25; } else if (brightness <= 0.5f) { resId = r.drawable.light_50; } else if (Brightness < 1.0f) { resId = r.dr awable.light_75; } else { resId = r.drawable.light_100; } Updateviews (resId, (int) (brightness *));
We are in the case of a relatively good cut.
Copy Code 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 public class Lightnesscontrol {11//determines if auto-brightness is turned on, the public static Boolean isautobrightness (Activity Act) { Boolean automicbrightness = false; Contentresolver acontentresolver = Act.getcontentresolver (); try {automicbrightness = Settings.System.getInt (Acontentresolver, Settings.System.SCREEN_BRIGHTNESS_MODE) = = Settings.system . screen_brightness_mode_automatic; * catch (Exception e) {toast.maketext (act, "Cannot get Brightness", toast.length_short). Show (); 20} 21 return automicbrightness; 22} 23//change brightness of public static void Setlightness (Activity act,int value) {2 7 SysTem.putint (Act.getcontentresolver (), system.screen_brightness,value); Windowmanager.layoutparams LP = Act.getwindow (). GetAttributes (); Lp.screenbrightness = (value<=0?1:value)/255f;30 Act.getwindow (). SetAttributes (LP); 31 } catch (Exception e) {toast.maketext (act, "Cannot change brightness", toast.length_short). Show (); 33} 34 }35//Get brightness of public static int getlightness (Activity Act) PNS {system.getint return (act.getcontent Resolver (), system.screen_brightness,-1); 39}40//Stop automatic brightness adjustment in the public static void Stopautobrightness (Activity a ctivity) {Settings.System.putInt (Activity.getcontentresolver (), Settings.System.SCREEN_BRIG Htness_mode, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); 45}46//Turn on brightness Auto adjust startautobrightness public static void (activity activity) {SETTINGS.SYSTEM.P Utint (Activity.getcontentresolver (), 49 Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMAT IC); 51} 52} Copy code this is a separate working class, mainly used to adjust the screen brightness, there are some comments, explain a few concepts, nonstandard, for reference: Contentresolver class, provides methods for accessing external shared data from other applications, such as the SYS used to obtain and set the brightness above Tem.getint (), System.setint (). Activity class, the main class of Android program, an interface must have this class to provide background support, need to inherit this class. Settings class, Android program system Related settings class, various settings can be found here. Layoutparams class, Android interface related parameters, such as height, width, brightness and so on. A toast class, a lightweight control that automatically disappears the hint box.
OK, the brightness adjustment of the screen is here, very simple.
Android Development adjusts the screen brightness