Summary of Android programming methods to realize night mode

This paper describes the method of implementing night mode by Android programming. Share with you for your reference, as follows:

With more and more functions realized by app, such as reading novels, watching videos, surfing the Internet, etc., many people now spend more and more time on mobile terminals such as mobile phones and tablets. However, the screens of mobile phones and tablets are not as durable as the ink screens of e-paper books such as Kindle. Due to the self luminous screen characteristics, we tend to have sore and tired eyes when staring at the screen for a long time, so there are various eye protection modes, ***

1. Use screen brightness

When using mobile phones and other terminals at night, directly reduce the brightness of the screen, which can reduce the stimulation of light intensity to glasses. This is also the simplest and relatively effective way

Please add corresponding permissions first:

There are two methods of setting screen brightness:

1) Set only the brightness within the application

Generally, each activity in Android corresponds to a visual interface. Set the brightness for each activity as follows:

In this way, we should set the brightness of all activities contained in a program separately. Although it can be encapsulated into tool classes for use, the premise is whether there is a better method? Look at the second:

2) Set the brightness of the mobile phone system (Global brightness)

Here, we directly set the brightness of the whole mobile phone in an activity of the program, such as the portal activity. Since the global brightness of the mobile phone has been set, the brightness of the mobile phone will still be the set brightness no matter which interface we jump to or even exit the program. This method is "once and for all" compared with the first method. But here we need to clarify our ideas and consider several points:

After opening the application, get the original brightness value of the mobile phone and save it (which can be used to restore the normal brightness after exiting the application) - > if the mobile phone turns on automatic brightness adjustment, turn off the automatic adjustment, and then set the appropriate lower brightness -- > apply the set brightness value to the mobile phone -- > finally, when exiting the application, use the saved original brightness value to restore the original brightness, And turn on the automatic brightness adjustment of the mobile phone again

Next, paste the key code:

First, get the brightness value of the mobile phone screen:

Save it and put it directly in sharepreference without writing the relevant code. Then check whether the phone turns on the automatic brightness adjustment switch:

If on, close:

Finally, turn on brightness adjustment:

After turning off the brightness adjustment, set the brightness directly by using the relevant code in the first method. However, to apply the brightness setting to the global, you need to save the brightness value to the phone:

In this way, the final effect is OK. Even if you exit the current application, the mobile phone still has the set low brightness. Of course, you should restore the normal brightness and settings before exiting the application, so you can use the saved brightness value to reset, and then save the new brightness value to the mobile phone again. Don't forget that if the mobile phone turned on the automatic brightness adjustment before, you have to turn on the automatic brightness adjustment again

2. Customize theme. (the most commonly used method)

I believe many people are familiar with custom view. Similar to this, custom theme is also the most commonly used method to realize the night mode, because it can not only realize the night mode, but also realize the common theme replacement function. I won't elaborate here, just talk about ideas. Suppose our application interface has a white background and black text, and the night mode is a black background, Grayish white text. This night mode is different from the first one. Because the color and transparency of the background and content text can be set at will, this night mode looks more intuitive and more comfortable

The custom theme uses the background and text color attributes defined in XML, such as:

Then create your own two themes in style.xml, such as the default theme and the night theme. The default theme is given to activity_ The background property is set to white, text_ The color attribute is set to black, and the night theme is black and grayish white respectively. In the view layout file, set the background view used, such as the backgroundColor attribute of a relativelayout to "? Activity_background", and the textcolor of textview to "? Text_color". Of course, because this is theme, It will take effect before Activity initiates the view, so it is best to wrap a theme tool class on its own and invoke the setTheme () method before setting the theme before the setContentView () method of Activity.

3. WindowManager implements mask mode

Here, we should understand the concept of window. In the design concept of Android, almost every displayed component is set to be included in a window. Activity also has its own window. By adding a gray black view with certain transparency to the window, it looks like the screen is darkened. Of course, the brightness of the mobile phone does not change, This implementation can be called "Mask", which is similar to putting a layer of film or lens on the lens when the camera takes pictures, so as to make the presentation effect different. However, this method also has some disadvantages, that is, it is similar to the above-mentioned method to set its brightness separately in each activity. Here, every time you enter an interface, you need to "put a layer of view" again. Compared with the "once and for all" method, It seems to have no advantage. Then go directly to the code:

In the code, the touch and focus of the added mask layer are prohibited through the parameter setting of windowmanager.layoutparams. In this way, even if a layer of view is added, the normal use of components in the activity view will not be affected

In fact, the above three methods are not new technologies for now, and I have seen a lot of relevant codes on the Internet. Here, they are just put together for comparison and summary. The above is based on the three @ R_ 404_ 1957 @ said their own characteristics. Generally speaking, the first and second methods are preferable. Whether to choose the first or the second method should depend on the specific requirements. If your application simply requires to reduce the brightness and does not want to modify too many codes, the first method will be more suitable than the first method; If you want to have a good experience, look cooler, or even add other themes, such as blue and green themes, there is no doubt that the second is the best choice. The wide application of custom attributes provides convenience for us to achieve more personalized visual effects (such as custom components, custom themes, etc.)

More readers interested in Android related content can view the special topics of this site: introduction and advanced tutorial of Android development, summary of Android debugging skills and common problem solving methods, summary of Android basic component usage, summary of Android view skills, summary of Android layout skills and summary of Android control usage

I hope this article will help you in Android programming.

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
分享
二维码
< <上一篇
下一篇>>