Several implementation methods of Android brightness adjustment

Recently, I was working on an app setting item, brightness adjustment. When I did it, I found that Android brightness adjustment was more complex than expected. In fact, there are a lot of information about this on the Internet, but some blog posts are misleading. Here, I will sort out this content according to my own understanding.

Overall, Android brightness adjustment is divided into three levels: Android system brightness adjustment, Android App brightness adjustment and Android current screen (window) brightness adjustment.

1. Android system brightness adjustment

The brightness adjustment of Android system is the most global, which is common in the brightness setting items in system settings. Android provides an interface to obtain and set the system brightness value ("brightness value in manual mode"), as follows:

Note that the returned luminance value is an integer value between 0 and 255.

In systems after Android 2.1, the "automatic brightness" option has been added to the system brightness adjustment. "Automatic brightness" automatically changes the brightness of the system according to the external light source. At present, most mobile phones can also adjust the value of "automatic brightness" in a small range. Corresponding to automatic brightness is "manual brightness". When it is under "manual brightness", setting and dragging the brightness progress bar will greatly change the brightness of Android system. "Manual brightness" and "automatic brightness" are called "brightness mode" of Android system respectively.

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

Unfortunately, the brightness value interface in "automatic brightness" mode is not provided in Android. The above-mentioned interface for obtaining system brightness value actually refers to the brightness value in the "manual brightness" mode.

Generally speaking, through the manual brightness value and setting the system brightness mode interface, most of the conventional brightness setting coding requirements for Android system can be met to complete the system brightness adjustment.

2. Android App brightness adjustment

Different from the system brightness, Android does not directly provide brightness adjustment methods at the app level. Therefore, the brightness adjustment of the app can be realized indirectly through the brightness adjustment of the system or the brightness adjustment of the current screen.

3. Android current screen (window) brightness adjustment

Android provides an interface for setting brightness for the current screen (window), which is commonly written as follows:

Note that the brightness here is a float type value between 0.0 and 1.0.

By default, when we directly modify the system brightness value, the brightness effect can be reflected in the current window, because by default, WindowManager The default value of screenbrightness of layoutparams is WindowManager LayoutParams. BRIGHTNESS_ OVERRIDE_ NONE。

That is, window does not have its own brightness parameters, which will change with the brightness effect of the system. This is the most common: after adjusting the system brightness, all windows will immediately reflect the system brightness setting effect.

At that time, we also encountered such requirements in the actual project: the brightness setting of the system is only effective for the current window or app, and does not affect the brightness setting of the system itself.

Suppose there is a seekbar in the current window, and the UI is basically similar to the system brightness adjustment UI. Users can slide the seekbar to change the brightness of the current window immediately without affecting the system brightness effect. How to achieve it?

At this point, we need to enable WindowManager The screenbrightness parameter of layoutparams has an automatic specific brightness value. After this value is set, it will override the system brightness setting within the current window range.

Therefore, it is necessary to convert the brightness value selected by the user into the corresponding window brightness value (in order to be consistent with the system brightness value, it is assumed that the maximum value of seekbar is 255).

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

Then, why is there a judgment of "brightness = = - 1" in the above code? This is mainly to consider the possible settings of "follow system brightness" or "restore system default brightness" in app settings. When users perform such operations, they can directly restore the screenbrightness parameter to the default parameter value. Because the brightness value of the system in the "automatic brightness" mode described above cannot be obtained directly, when the system is in the "automatic brightness" mode, the brightness parameter value will not be determined accurately. Therefore, it is an effective method to restore the screenbrightness parameter to the default parameter value.

4. Analysis of feasible schemes for Android App brightness adjustment

At present, when many blogs on the Internet mention app brightness adjustment, the proposed scheme is to first record the system brightness value and brightness mode before setting in the app setting item. When users adjust the brightness in the app setting item, they directly modify the system brightness value. When users exit the app or the app is in the background (such as pressing the home button), then restore the system brightness. At first glance, it seems a feasible scheme. However, there are mainly the following problems:

1. How to obtain the system brightness value and brightness mode before setting (because the system brightness value needs to be restored to this initial value outside this app later)? When the user enters the settings page each time, get the settings page? Strictly speaking, it can't be accurately recorded. Because the user operation of Android is unpredictable, such as entering the settings page and dragging seekbar to set a brightness value, the brightness value of the system is directly modified. If the user directly modifies the system brightness setting outside the app without putting the application in the background or exiting the application, for example, in Xiaomi, you can pull down the title bar, You can set the system brightness directly. Therefore, it is difficult to obtain the initial value of system brightness before app brightness setting.

2. How to judge whether the user is outside the app? Because it is necessary to restore the system brightness to the initial system brightness. For example, the user can press the home button, long press the home button to directly switch the app, directly press the back button to exit this app step by step, directly click other app notification information in the drop-down title bar to enter other apps, and the user can directly enter other applications when the mobile phone is automatically locked and unlocked. Such operation scenarios are also unpredictable. Therefore, It is also difficult to judge when the user comes outside 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 the user adjusts the brightness in the setting item, calling the changeappbrightness() method will change the brightness of the current screen (window). At this time, it will have no impact on the system brightness. The next problem will finally focus on when the user comes to other activities in the app, if the brightness value just set is reflected in real time.

After the user adjusts the brightness, save the current brightness setting value (for example, in SharedPreferences). In the onresume method of the base class activity, you can take out the app brightness value set by the user in SharedPreferences, and then change the appbrightness() method to adjust the brightness of each current screen.

Generally speaking, setting app brightness by setting the current screen (window) is simpler and more effective.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>