TextView implements automatic line feed

Source: Internet
Author: User

Package com. liao. intentservice;
 
Import java. util. Arrays;
 
Import android. content. Context;
Import android. content. res. Resources;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. graphics. Paint. FontMetrics;
Import android. graphics. Paint. Style;
Import android. util. AttributeSet;
Import android. util. TypedValue;
Import android. view. View;
 
 
Public class AutoText extends View {
Private Paint mPaint = new Paint ();
 
Public AutoText (Context context ){
This (context, null );
}
 

Public AutoText (Context context, AttributeSet attrs, int defStyle ){
Super (context, attrs, defStyle );
Init ();
}
 
Public AutoText (Context context, AttributeSet attrs ){
This (context, attrs, 0 );
}
 
Private void init (){
MPaint. setAntiAlias (true );
MPaint. setColor (Color. RED );
MPaint. setStyle (Style. STROKE );
MPaint. setTextSize (getRawSize (TypedValue. COMPLEX_UNIT_DIP, 15 ));

}
 
@ Override
Protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec ){
Super. onMeasure (widthMeasureSpec, heightMeasureSpec );


// SetMeasuredDimension (300,200 );
}

@ Override
Protected void onDraw (Canvas canvas ){
Super. onDraw (canvas );

// View. draw () draws the control background

// Draw operations and sequence of controls:
/*
* Draw traversal performs several drawing steps which must be executed
* In the appropriate order:
*
* 1. Draw the background (drawing the background set by the Control)
* 2. If necessary, save the canvas 'layers to prepare for fading
* 3. Draw view's content (it can be rewritten, onDraw (canvas );)
* 4. Draw children (can be rewritten to distribute the canvas to the Child control. For details, see ViewGroup. Corresponding Method dispatchDraw (canvas); this method calls the draw () method of the Child control in turn)
* 5. If necessary, draw the fading edges and restore layers (draw the shadow gradient effect around the control)
* 6. Draw decorations (scrollbars for instance) (used to Draw a scroll bar, corresponding to onDrawScrollBars (canvas );.
* You can override onDrawHorizontalScrollBar () and onDrawVerticalScrollBar () to customize the scroll bar)
*/

// You can draw content and scroll bars.

// Draw backgroud
Canvas. drawColor (Color. WHITE );

// Draw text
FontMetrics fm = mPaint. getFontMetrics ();

Float baseline = fm. descent-fm. ascent;
Float x = 0;
Float y = baseline; // because the system draws text based on the bottom of the font, the height of the font must be added.

// String txt = getResources (). getString ("asda ");
String txt = "Bachelor degree or above in computer science or related fields, with at least 2 years of relevant work experience; 2. Solid technical skills in computer technology field, especially in terms of data structure, algorithms, code, and software design, "+
"+ 3. Familiar with distributed computing, network system design, database design and large-scale storage systems, skillful C/C ++ programming skills" +
"4. Be familiar with HTTP protocol and develop HTTP-based applications. Experience in online applications and online game development is preferred." +
"5. Experience in Web service/Ajax/JavaScript/Apache/Tomcat/Struts/iBatis/Spring/PHP/JSP/Python is preferred." +
"6. Familiar with TCP/IP protocol, socket and multi-thread development, with high-traffic network development experience. ";

// Text wrap
String [] texts = autoSplit (txt, mPaint, getWidth ()-5 );

System. out. printf ("line indexs: % s \ n", Arrays. toString (texts ));

For (String text: texts ){
Canvas. drawText (text, x, y, mPaint); // The coordinates are the origin points in the upper left corner of the control.
Y + = baseline + fm. leading; // Add the font row spacing.
}
}


/**
* Automatically split text
* @ Param content refers to the text to be split.
* @ Param p paint brush, used to measure the text width based on the font
* @ Param width the specified width
* @ Return a string array to save the text of each line
*/
Private String [] autoSplit (String content, Paint p, float width ){
Int length = content. length ();
Float textWidth = p. measureText (content );
If (textWidth <= width ){
Return new String [] {content };
}

Int start = 0, end = 1, I = 0;
Int lines = (int) Math. ceil (textWidth/width); // calculates the number of rows
String [] lineTexts = new String [lines];
While (start <length ){
If (p. measureText (content, start, end)> width) {// when the text width exceeds the control width
LineTexts [I ++] = (String) content. subSequence (start, end );
Start = end;
}
If (end = length) {// text with less than one line
LineTexts [I] = (String) content. subSequence (start, end );
Break;
}
End + = 1;
}
Return lineTexts;
}

/**
* Obtain the original size of a specified unit (based on device information)
* Px, dip, sp-> px
*
* The unit of Paint. setTextSize () is px.
*
*
*
* Code excerpt: TextView. setTextSize ()
*
* @ Param unit TypedValue. COMPLEX_UNIT _*
* @ Param size
* @ Return
*/
Public float getRawSize (int unit, float size ){
Context c = getContext ();
Resources r;
 
If (c = null)
R = Resources. getSystem ();
Else
R = c. getResources ();

Return TypedValue. applyDimension (unit, size, r. getDisplayMetrics ());
}
}

From the column of android interests

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.