Adjust screen brightness for Android Development

Source: Internet
Author: User

Adjust screen brightness for Android Development

In the player, we can often see that the screen brightness can be adjusted by sliding up or down a part of the screen, slide up or down a part to adjust the playback volume. The playback progress can be adjusted by sliding left and right.

Today, I want to adjust the brightness. In fact, it is mainly implemented by setting the View attributes.

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;} callback mlightchange (params. screenBrightness );}

The above code is called when the left part slides up or down.

The mligthchange code 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.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));    }

Let's take a relatively good example.

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 10 public class LightnessControl {11 // determines whether automatic brightness adjustment 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, "brightness cannot be obtained", Toast. LENGTH_SHORT ). show (); 20} 21 return automicBrightness; 22} 23 // brightness 24 public static void Set Lightness (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, "brightness cannot be changed", Toast. LENGTH_SHORT ). show (); 33} 34} 35 // obtain the brightness 36 public static int GetLightness (Activity act) 37 {38 return System. getInt (act. getContentResolver (), System. SCREEN_BRIGHTNESS,-1); 39} 40 // stop automatic brightness adjustment 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 // enable automatic brightness adjustment 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} copy the Code. This is a standalone class used to adjust the screen brightness. Some comments are provided. Concept, not standard, for reference: ContentResolver class, provides methods for accessing external shared data of other applications, such as the System used to obtain and set the brightness above. getInt (), System. setInt (). Activity Class, the main class of the android program, an interface must provide such background support, need to inherit this class. Settings class, which is related to the android program system. You can find all the Settings here. LayoutParams class, android Interface related parameters, such as height, width, brightness, etc. Toast class, a prompt box that can automatically disappear, lightweight controls.


Well, it's easy to adjust the brightness of the screen.






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.