Android screen fillet implementation method example code

Now, the four corners of the screen of many full screen mobile phones are made into a circle, and its mellow feeling brings people a different visual experience. First, let's take a picture of my big hammer town building (it's not an advertisement, the hammer didn't give money). Let's intuitively feel the charm of the round corner.

Hammer.jpg

Of course, this is implemented in hardware. I suspect it is also a square display screen, and then a fillet masking is made. Can't we smile and circle for our face square mobile phones? The answer is yes, no picture say

design sketch

(I should be able to see that I use Shenma mobile phone)

Fillet implementation:

Then we should think about how to achieve this effect at the software level. I believe many friends will immediately think of using floating windows.

Implementation principle: use WindowManager to add our fillet to the four corners of the screen, and set the fillet color to black to form a visual fillet screen.

1. Custom fillet view

Obviously, first of all, we need to implement a fillet with the shape as shown in the figure below. What should we do? Use path (unfamiliar friends can baidu HA). Here we take the upper left corner as an example to realize this fillet.

In order to allow users to customize the fillet color, size, transparency and whether each corner is displayed, I have added corresponding control variables to update the view when the user sets it.

Is it very simple? Such a rounded view is implemented.

2. Display fillet

Now we're going to add rounded corners to the four corners of the screen. Android's floating window technology is used here. That is, use windows manager to add our rounded corners to the top window to mask other parts. A windowmanager.layoutparams needs to be set when Windows manager adds a view. Let's initialize this params:

The notes are written in detail. If you don't understand, baidu ha.

Then we need to add rounded corners to the screen:

The above code adds our corner to the screen according to whether each location can be displayed. Buildcorner creates a cornerview according to user-defined settings

At this point, our rounded corners are displayed on the screen (applause).

Fillet keeping alive:

Firstly, in order to make the fillet exist on the screen for a long time, I put its operations (add, delete and customize) into a service, but the service is also easy to be killed. Here we need to keep it alive.

There are many keep alive schemes in Android, such as using service startforegroup, dual processes, one pixel activity, receiving system broadcast, starting service, using jobsheduler, timer, etc.

It is recommended to read this article: https://www.oudahe.com/p/26112/ 。

Here I use the first scheme, that is, when the user chooses to keep alive, use service startforegroup to send a notice to ensure that our service will not be killed to the greatest extent. The code is relatively simple, so it won't be posted.

Some small details:

During the test, I found that after forcibly stopping the application service and entering the application again, we can't get rid of the fillet when we want to remove it. This problem also exists in some commercial applications. This is because I didn't remove the view we added before. I added another layer when I reopened the application. So we need to remove our view before destroy, and then the function will be normal.

Another problem is the application of permission. When we add a floating window, we should check whether we have permission. The code is as follows

Reflection is used here, and thanks to the online God's solution.

Summary:

The first time I wrote a blog, I was unfamiliar and made mistakes. I hope you can understand and correct them (I'm ready to welcome your saliva).

Hope to learn and make progress together

This is my project address. If necessary, you can have a look: https://github.com/zibuyuqing/master/tree/master/RoundCorner

Finally, attach a screenshot of the app to celebrate

design sketch

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