Several methods of realizing the brightness adjustment of Android _android

Source: Internet
Author: User

Recently doing an app setting, brightness adjustment. In real time, it turns out that the Android brightness adjustment is a bit more complicated than expected. In fact, there are already many information on the Internet, but some of the posts are misleading. Put this piece of content in your own understanding.

Overall, the Android brightness adjustment is divided into three levels: the Android system brightness adjustment, the Android app brightness adjustment and the Android current screen (Window) brightness adjustment.

1.Android System Brightness Adjustment

The Android system has the highest brightness adjustment globally and is common in the Brightness settings in system settings. Android provides an interface for getting and setting system brightness values ("Brightness values in manual mode"), as follows:

Get the System brightness
Settings.System.getInt (Getcontentresolver (), Settings.System.SCREEN_BRIGHTNESS);
Set the system brightness
Settings.System.putInt (Getcontentresolver (), settings.system.screen_brightness,systembrightness);

One of the things to note is that the brightness value returned is an integer value between 0-255.

In the system after Android 2.1, the "Automatic brightness" option is added to the system brightness adjustment. "Automatic Brightness" is based on the external light from the move to change the brightness of the system, the majority of mobile phones in the "automatic brightness" can also be a small amplitude of the adjustment of its value. corresponding to the automatic brightness is "manual brightness", when under "Manual brightness", set the drag brightness progress bar will significantly change the brightness of the Android system. "Manual Brightness" and "automatic brightness" are called the "brightness mode" of the Android system respectively.

Correspondingly, the Android system also provides an interface to get and set the brightness mode.

Gets the system brightness mode
Settings.System.getInt (Getcontentresolver (), Settings.System.SCREEN_BRIGHTNESS_MODE);

Set the system brightness mode
Settings.System.putInt (Getcontentresolver (), Settings.System.SCREEN_BRIGHTNESS_MODE, Systemmode);

Unfortunately, the brightness value interface in auto brightness mode is not available in Android. The above mentioned to obtain the system brightness value interface is actually refers to the "manual brightness" mode of the brightness value.

In general, through the manual brightness value and set the system Brightness mode interface, you can meet the majority of the conventional Android system brightness coding requirements to complete the system brightness adjustment.

2.Android app Brightness adjustment

Unlike system brightness, Android does not directly provide a way to adjust the brightness of the app level. Therefore, the need for the app's brightness adjustment, can be achieved through the system brightness adjustment or the current screen brightness adjustment method indirectly.

3.Android Current screen (Window) Brightness adjustment

Android provides a luminance-setting interface for the current screen (Window), commonly written as follows:

window window = Activity.getwindow ();
Windowmanager.layoutparams LP = Window.getattributes ();
lp.screenbrightness = brightness;
Window.setattributes (LP);

One of the things to note is that the brightness here is a float type value between 0.0-1.0.

By default, when we directly modify the system brightness value, the current window can immediately reflect the brightness effect, because by default, The default value for the Windowmanager.layoutparams screenbrightness is WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE.

That is, the window does not have its own brightness parameters, the following system will change the brightness effect. This is also our most common: when the system brightness is adjusted, all windows immediately reflect the system brightness setting effect.

At that time, we also encountered such requirements in the actual project: the system set brightness only for the current window or app, without affecting the system itself brightness settings.

Assuming that there is a seekbar,ui in the current window that is basically similar to the system brightness adjustment UI, the user can slide this seekbar so that the current window brightness changes immediately and does not affect the system brightness effect. How to achieve it?

At this point we need to enable the Windowmanager.layoutparams screenbrightness parameter, so that it has an automatic specific brightness value, set this value in the current window range, it will override the system brightness settings.

Therefore, it is necessary to convert the brightness value selected by the user to the corresponding window brightness value (in accordance with the system brightness value, assuming the maximum value of Seekbar is 255).

Modifies the current Window brightness public
void changeappbrightness (context, int brightness) {
  window window = () based on the Brightness value ( Activity). GetWindow ();
  Windowmanager.layoutparams LP = Window.getattributes ();
  if (brightness = = 1) {
    lp.screenbrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
  } else {
    lp.screenbrightness = (brightness <= 0? 1:brightness)/255f;
  }
  Window.setattributes (LP);
}

Where the brightness parameter is the brightness value selected by the user.

So why is there a "brightness = 1" judgment in the above code? This is primarily to take into account settings such as "Follow system brightness" or "restore system default Brightness" that may appear in the app settings item, and when the user does this, the screenbrightness parameter is restored to the default parameter value directly. Because the system brightness value is not directly derived from the previous description of the "Automatic Brightness" mode, when the system is in "automatic brightness" mode, the brightness parameter value will not be accurately determined, so Restoring the screenbrightness parameter to the default parameter value becomes an effective method.

Analysis of feasible scheme for 4.Android app brightness adjustment

There are many blog posts mentioned in the app brightness adjustment, the proposed scheme is in the app settings, the first record of the system before setting the brightness value and brightness mode, the user in the app settings for brightness adjustment, directly modify the system brightness value, when the user quit this app, Or app for the background (such as pressing the home button, etc.), and then restore the system brightness. At first it looks like a workable solution. But there are several main problems:

1. How do I get the system brightness value and brightness mode before setting (because after this app you want to restore the system brightness value to this initial value)? When the user gets into the settings page each time? Strictly speaking, it is impossible to record accurately. Because the Android user operation is unpredictable, such as access to the Settings page, drag Seekbar set a brightness value, at this time directly modified the system brightness value, if the user at this time did not apply to the background or the application is not to quit the case directly outside the app to modify the system brightness settings, such as millet can be pulled through the title bar, directly can set the system brightness. Therefore, it is difficult to obtain the initial value of the system brightness before the app brightness setting.

2. How to judge the user came to the outside of the app? Because you need to restore the system brightness setting to the initial system brightness at this time. If the user can press the home button, long press the home button directly switch app, directly back button and so on step out of this app, Drop-down title bar directly click on other app information into other app, the phone automatically lock screen after unlocking users directly into other applications and so on, This kind of operation scenario is also unpredictable, so it is difficult to judge the time when the user comes to the outside of the app to restore the system brightness to the initial value.

Therefore, the app brightness adjustment scheme is recommended by setting the current screen (Window).

The general idea is as follows: When a user adjusts for brightness in a set item, calling the Changeappbrightness () method changes the brightness of the current screen (Window), which has no effect on the brightness of the system. The next problem is finally to focus on when users come to other activity in this app, if the brightness you just set is worth reacting to instantly.

When the user makes the brightness adjustment, saves the current brightness setting value (such as saving to sharedpreferences), in the Onresume method in the base activity, you can remove the app brightness value set by the user in the sharedpreferences. The Changeappbrightness () method is then used to achieve the brightness adjustment for each current screen.

Overall, it is simpler and more efficient to set the app brightness by setting the current screen (Window).

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.