Android custom view can slide left and right to select the year of birth

The third part of customizing the view imitates the personal birth date setting view in the microblog sports interface. First look at my renderings:

It supports setting the initial year, sliding left and right to select the year of birth, and the value of the corresponding textview will also change. This animation effect has been made for a long time, but it still feels stiff, which is a little different from that on microblog. We have improved plans. Welcome to communicate with us.

The custom view trilogy is still the same here. Let's see how to implement it.

1. Customize view attributes: create an attrs.xml under RES / values / to define our attributes and declare our entire style.

Three attributes, font size, font color and line color, are defined successively. Format is the value type of this attribute. Then declare our custom view in the layout file:

We can set the custom view attribute ourselves. Remember to introduce our namespace, xmlns: app = " http://schemas.Android.com/apk/res-auto ”

2. Get the properties of the custom view:

Custom views generally need to implement the following three construction methods, which are called one layer at a time and belong to a progressive relationship. Therefore, we only need to obtain the view properties and perform some necessary initialization operations in the last constructor. Try not to instantiate objects in the OnDraw process, because it is a frequently repeated process, and new needs to allocate memory space. If a large number of new objects are used in a frequently repeated process, memory will be wasted.

3. Override the onmesure method to determine the view size:

The last article on user-defined view is very detailed, which will not be repeated here. The focus is on the OnDraw method: Android user-defined view imitates the animation effect of microblog motion points

4. Rewrite OnDraw method to draw:

As mentioned earlier, for complex custom views, before rewriting the OnDraw method, first draw the general appearance in the draft book, and simply mark the coordinates and starting point. This method is very practical and clear.

The position of point a is the initial position for drawing numbers, and the position of point B is the initial position for drawing vertical lines. To determine these two initial positions, we just need to write a cycle, find the law, and draw the following lines and words in turn. Because the event processing involves sliding left and right, the knowledge of Android event distribution is required for processing.

Rewrite the dispatchtouchevent method of view to distinguish between left and right sliding. Mtouchslop is the default minimum sliding distance of Android. If the sliding distance in the horizontal direction is greater than that in the vertical direction, it is judged as horizontal sliding. Here, in order not to make the sliding so obvious, I make the horizontal sliding distance greater than twice the default distance to determine the left-right sliding. State is the state of record sliding.

Override the ontouchevent method of view to handle the click event of view. (1) In the demo dynamic graph, in the process of sliding left and right, the middle number will change from large to small, and the left and right numbers will change from small to large. Bigtxtsize represents the size of the middle number, onesize represents the size of the second number from left to right, and thirdsize represents the size of the fourth number from left to right. During the sliding process, the postinvalidate () method is used to call the OnDraw method all the time to re draw, so as to achieve the effect of number size change. (2) Judge after sliding. If it is sliding from left to right, the number will be reduced by one; If you slide from right to left, you will add one to the number. Finally, restore the number size and sliding state to the default value. (3) Finally, be sure to return true, indicating that the current sliding event is consumed, otherwise the sliding will not respond

After all the sliding operations have been handled, the next step is to draw:

In fact, here is to get the digital size of the sliding operation, and then draw it. Finally, add one number each time. Linex is the initial position of point B, and add one fifth of the width each time.

5. Get the current setting value. You can see that the textview above the view will also change with the value set below, so we need to deal with it separately here. Interface callback, a simple and violent way.

In the case motionevent.action of ontouchevent_ Up, get the last set value

listener.OnScroll(size);

Then there is the corresponding activity:

Implement the method of the interface, initialize, set the initial value, and then update the data in the method of the interface.

The first article of custom view: Android custom view realizes BMI index bar

The second part of custom view: Android custom view imitation microblog motion integral animation effect

OK, see you next time.

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