Android makes a simple vertical pull-up and slide down effect

1、 Introduction

Recently, a friend company needs to implement a vertical pull-up and slide down view. Initially, only part of the view is displayed at the bottom of the screen. Pulling up that part can pull out all the view and display it on the screen. Sliding down the view can hide the view under the screen. Let's take a look at the final effect.

2、 Realization idea

1. In fact, there are many ways to achieve this effect. In order to have a slow sliding effect like viewpager when I let go, I choose to use scrollby with scroller, which should be both convenient and practical. 2. The design of this view is as follows: (1) place the sub view of this view under the view through layout; (2) By overriding the ontouchevent method, give the sub view a sliding effect in move_ The up action adds a scroller to the sub view to smooth it to the top or bottom of the view. See Figure:

3、 Realize

1. First, customize an attribute to indicate how many parts of the sub view should be exposed, that is, the part where red and green intersect in the above figure. Create an attrs. XML file under the res - values folder

attrs.xml :

Reference this attribute in the XML file:

Call this property in code (the name of View is MyScrollerView, and I can easily inherit LinearLayout, inheritance ViewGroup or other layout View).

2. Rewrite the onfinishinflate method. The reason for rewriting this method is that I want to have only one child view, so as to determine the sliding height. Otherwise, I need to recalculate the total height of the child views, which is more troublesome. This method will be called before onMeasure.

3. Init method performs some initialization operations, such as creating a scroller object and making the background of view transparent:

4. Override the onmeasure method and onlayout method to determine the maximum height that can be slid and the arrangement position of sub views (in fact, you don't need to override onmeasure. I'm just used to writing this).

5. Let's first look at the meaning of the member variable I defined:

6. Rewrite the ontouchevent method, make sliding judgment, and the explanations are written in the notes:

7. Finally, you need to rewrite a computescroll method, which is used to cooperate with the scroller:

8. For the usage of scroller, please refer to Guo Lin's blog: http://blog.csdn.net/guolin_ blog/article/details/48719871

4、 Full code:

xml:

MyScrollerView:

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