Android custom view implementation verification code

This article is based on some extensions of Hongyang's Android custom view (I), as well as some reprints of the detailed explanation of the Android custom view constructor.

First, we define a declare styleable tag, which is used to add custom properties to custom controls. For example, we define the color, size, length and background color of text

Android provides methods to customize attributes. The format parameters include (reference, color, Boolean, dimension, float, integer, string, fraction, enum, flag)

1. Reference: resource ID: if this attribute is set, it is equivalent to calling resource files such as @ string | @ drawable. 2 Color: this attribute is used to set the color value of 8 or 6-bit hexadecimal color value. For example, setting textcolor of textview has the same effect (such as #ff000 setting it to red). 3 Boolean: this parameter is used to set true or false 4 Dimension: this parameter is used to set dimension values, such as Px, dip, DP, SP, etc. 5 Float: this parameter is used to set floating point data 6 Integer: this parameter is used to set the integer data 7 String: this parameter is used to set string data, such as textview's text property 8 Fraction: this parameter is used to set percentage data. 9: enum: this parameter is equivalent to setting fixed parameters for the name attribute of this attr. For example, the orientation attribute of linear layout can only be set to vertical or horizontal 10: Flag: this parameter is used to: bit or operation

The steps for customizing a view are

1. Customize the properties of view 2. Obtain our customized properties in the view construction method 3. Rewrite onmeasure 4. Rewrite OnDraw

Sometimes the onmeasure method does not need to be rewritten, such as the system's own components, and then we define the required attributes

Then we rewrite the three construction methods. What we need to pay attention to is

1. When you directly create a custom view instance in the code, the first constructor will be called 2, when the custom view is invoked in the XML layout file, second constructors will be called. 3, in the XML layout file to call custom view, and custom tags in the custom attribute, here calls second constructors.

That is to say, the system will only call the first two constructors of Custom View by default. As for the call of third constructors, we usually call them in the constructor automatically (for example, calling third constructors in second constructors). As for the acquisition of custom attributes, it is usually realized through the obtainstyledattributes function in the constructor.

The second attribute of obtainstyledattributes is the call you just made in attributes The name of the declare styleable tag in the XML file, and then we rewrite the onmeasure method to calculate the width and height through the getmeasuredlength method

Then called in the onmeasure method

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_ When content, we need to measure ourselves, that is, rewrite the onmeasure method

Before rewriting, first understand the specmode of measurespec. There are three types: actual: it usually sets 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, some beginners may not understand the role of getmode and getSize. First, getmode () is used to judge the mode of width and height setting. After obtaining it, you can judge, for example

Then we rewrite the OnDraw method

Multiple strings are drawn here, and each string is skewed. In this way, we can use randomtextstyle() to set each character to a different style every time we draw characters. Here we talk about several parameters of DrawText. The first parameter is the text content to be drawn, and the second parameter is the x-axis, which is equivalent to the left margin, The third parameter is the Y-axis and the fourth parameter is the instance of paint. My friend talked about the drawing coordinates of DrawText. If you are interested, you can take a look at the text center of Android canvas drawtext()

Finally, we reference our custom view in the layout file

Add xmlns: XX = "in the root layout http://schemas.android.com/apk/res-auto ”Here XX can be any letter, and then use xx to point. We set the value in the name of attr. Finally, the effect is like this

Since it is the verification code view, we naturally want to open the click event of clicking to change the verification code content, and add the click event in the third construction method

In this way, you can click to change the content of the verification code once, and we have opened two methods to judge the verification code or get the verification code. I'm just a simple verification code, and you can add more things yourself.

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