Android string processing

Source: Internet
Author: User

Requirement: implement the scrolling of long text, so you need to cut the long string into a string array with the specified TextView length, and then implement it using ViewFilpper.

The split code is as follows:
/** Split the String as required */public static String [] getLineStrs (String content, Paint paint, float width, float textSize) {paint. setTextSize (textSize); // Note1: The measurement tool must first define the unit int index = 0; int start = 0; int end = 0; float textLength = paint. measureText (content); int lineNum = (int) Math. ceil (1.5 * textLength/width); // Note2: the number of calculated rows is increased by 1.5 times because of inaccurate judgment. Finally, the String [] mSplitTextParts = new String [lineNum] is processed. for (int I = 0; I <= content. length (); I ++) {end = I; float measureLength = paint. measureText (content, start, end); // Note3: string in the range of [start, end) if (measureLength> = width) {mSplitTextParts [index] = content. substring (start, end); // Note4: string in the range of [start, end) start = end; index ++;} if (end = content. length () {mSplitTextParts [index] = content. substring (start, end); return Arrays. copyOf (mSplitTextParts, index); // Note5: the unassigned null value string must be cleared because the number of rows is inaccurate.} return null;

The reason for inaccurate speculation is that English and Chinese are mixed. It may be a problem of halfwidth and fullwidth.
After the test, we found that:
1. In full English, the standard number of rows calculated in the full-width corner (no more than 1.5 times of margin correction) are OK.
2. the standard number of rows calculated in the full-width and full-width mode (without any limit of 1.5 times) is OK.
3. In a mix of Chinese and English, the calculated standard number of lines is larger than the actual number of lines intercepted. There are doubts here.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.