Android control view creates a perfect custom sideslip menu

1、 Overview

In the app, there are often sideslip menus, sideslip view and other effects. Although Android has many third-party open source libraries, we can actually write a custom sideslip view control ourselves. In fact, it is not difficult. It mainly involves the following points:

1. Understand the decorview in the window class in Android

2. Achieve smooth movement effect on the scroller class

3. Implementation of custom ViewGroup

Let's first look at the renderings:

Now let's talk about the basic idea of implementing sideslip view. Here I use a custom control inherited from relativelayout called xcslideview class.

First, get a menuview from the inflator in the layout file, and then add the sideslip view to the custom control view through the addview method. How to hide the sideslip view of xcslideview out of the screen? Simply move the screen width by one distance through the scrollto method. Here, take the slide out on the left as an example. Just do xcslideview.this.scrollto (mscreen width, 0); Mscreen width is the screen width. The next thing to deal with is the translucent black mask effect at the bottom. This is actually a view, and then set the translucent effect. Of course, this is simple. The key is that we let it be displayed under our custom sideslip view. Here we first give a simple analysis of decorview to facilitate the following introduction to adding a translucent view mask:

The following is an explanation of the above figure:

1. Decorview is the topmost view of the entire window interface.

2. Decorview has only one child element, LinearLayout. Represents the entire window interface, including three areas: notification bar, title bar and content display bar.

3. There are two FrameLayout child elements in LinearLayout.

(20) Displays the interface for the title block. Only one textview displays the name of the application. You can also customize the title block. The loaded custom title block view will be added to the FrameLayout.

(21) display interface for content bar. This is the layout interface loaded by setcontentview () method and added to it.

With the above decorview knowledge background, let's talk about how to add a mask view and add a custom sideslip view to the decorview of the activity. First, add the mask view to the

(31) in customview, and then add the custom sideslip view to (21) FrameLayout. As for why, this is done because the width of the custom sideslip view is not necessarily the screen width. In addition, it also deals with whether there is a title bar and whether the immersive status bar design is adopted.

2、 Implementation of custom sideslip view

According to the above overview, you should know the general idea. Next, I will give the core code of the custom sideslip view class:

1. Variables used to customize sideslip view:

2. To initialize and create a custom sideslip view:

3. Create a translucent mask view and add it to the contentview

4. Set the sideslip menu view and add it to the decorview - > LinearLayout - > content display area view (FrameLayout)

5. Handle the sideslip and hide effects of the custom sideslip view:

3、 How to use this custom sideslip view control

It is easy to use. Create a sideslip view through the Create method, and then set a sideslip view through the setmenuview method. If it is necessary to set the width, set it through the setmenuwidth method, and finally slide it out with the show() method. Is it convenient to use?

4、 Source download

Download: sideslip menu

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