Implementation example of Android custom vertical textview

Implementation example of Android custom vertical textview

preface:

In the previous contact module, I encountered a left index control, in which the characters are arranged vertically. At that time, the control was replaced by an image. Now, if the characters of the index change, you have to change the picture again. It's very troublesome. Today, similar functions are implemented through a custom textview. Effect drawing first:

Both Chinese and English characters can be arranged vertically. Combined with the contact interface, you can change the index on the left to the last name of the contact.

Upper Code:

The activity used for the test.

Here is how to get the index value by clicking or sliding the user-defined textview on the left. The main implementation points are as follows:

Here, verticaltextviewheight is the height of the whole control, Y axis coordinates after pressing the control, and then the click position is converted into the corresponding character position in the vertical index character set through a proportional formula. Through this position, we can judge which index character is clicked. It should be noted here that the operation in the proportional formula should be converted into floating-point calculation, otherwise the correct index value cannot be obtained. The landlord had a deep pit here at that time.

The following is the implementation code for focusing on customizing textview:

The code is also very simple, copying the OnDraw function. Split the string to be displayed into characters and put them in an array. Go through the array in a loop and draw them one by one. The essence is only one sentence:

The first parameter is the character to be drawn. The second parameter draws the starting point of the x-axis of the character. The third parameter is complex, focusing on the first half of the second part

The y-axis coordinate of an office that represents the height of each character area, and the second half

This is a correction value. If you don't keep up with it, the drawn characters will be up as a whole. Here we need to refer to the drawing principle of characters. It will be very simple after understanding. I won't talk too much here.

Finally, the layout file of the test activity:

When the character set changes, it still adapts well. Effect picture:

Thank you for reading, hope to help you, thank you for your support to this site!

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