Android custom view to write random verification code

Many Android entry program apes may be afraid of Android custom view, but this is the only way for experts to advance. They are ready to spend some time and write more articles on custom view. First, summarize the steps to customize the view: 1. Customize the properties of the view. 2. Obtain our customized properties in the view construction method [3. Rewrite onmesure] 4. Rewrite OnDraw

I marked 3 with [], so 3 is not necessarily necessary. Of course, it still needs to be rewritten in most cases.

1. To customize the view attributes, first create an attrs.xml under RES / values / to define our attributes and declare our entire style.

We define three attributes: font, font color and font size. Format is the value. The value types of this attribute are: string, color, dimension, integer, enum, reference, float, Boolean, fraction and flag; If you don't know, you can Google. Then declare our custom view in the layout

Be sure to introduce xmlns: custom=“ http://schemas.android.com/apk/res/com.example.customview01 "Our namespace, and the package path behind it refers to the package of the project

2. In the view construction method, get our custom style

We have rewritten three construction methods. The default layout file calls the construction method of two parameters, so remember to let all constructions call our construction of three parameters, and we get custom properties in the construction of three parameters.

3. We rewrite OnDraw and onmesure to call the system provided:

The effect is:

Do you think it's pretty good? You've basically implemented a custom view. But at this time, if we write the width and height of the layout file as wrap_ Content, we will find that the effect is not what we expected:

The height and width measured by the system are match_ Parnet, when we set a clear width and height, the measurement result of the system is the result we set when we set wrap_ Content, or match_ Match is the measurement result of parent system_ Length of parent. So, when wrap is set_ During content, we need to measure ourselves, that is, rewrite the onmeasure method ": before rewriting, we need to understand the specmode of measurespec, which has three types:

Actually: it is usually set with an explicit value or match_ PARENT AT_ Most: indicates that the sub layout is limited to a maximum value, usually warp_ Content unspecified: indicates that the sub layout is as large as it wants, and is rarely used

Here is our code for rewriting onmeasure:

Now let's modify the following layout file:

The effect now is:

Completely compound our expectations. Now we can set the height and width casually, which can basically meet our needs. Of course, in this way, our custom view has no advantages over textview. Therefore, we think that adding an event to the custom view: adding:

Let's run again:

We added a click event to randomly generate a 4-bit random number each time. If you are interested, you can add a little noise in OnDraw and rewrite it into a verification code. Does it feel good.

Source code download: http://xiazai.jb51.net/201610/yuanma/AndroidCustomView (jb51.net).rar

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