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.