Refer to the online article:
Http://www.2cto.com/kf/201503/383397.html
Http://www.apkbus.com/android-176726-1-1.html
Public class cytextview extends textview {public static int m_ itextheight; // the height of the text public static int m_itextwidth;// the width of the text private paint mPaint = null; private String string = "";p rivate float linespace = 0;// Line Spacing Private context context; public cytextview ( Context context, attributeset set) {super (context, set);this.context = Context; typedarray typedarray = context.obtainstyledattributes (set, R.styleable.cytextview); Float textsize = typedarray.getdimension (R.styleable.CYTextView_ MYTEXTSIZE,&NBSP;20); Int textcolor = typedarray.getcolor (R.styleable.cytextview_mytextcolor, -1442840576); Float linespace = typedarray.getdimension (r.styleable.cytextview_ LINESPACINGEXTRA,&NBSP;-12); int typeface = typedarray.getcolor (r.styleable.cytextview_typeface, 0); LINESPACE&NBSP;=&NBSP;DENSITYUTIL.DIP2PX ( CONTEXT,&NBSP;-5); Typedarray.recycle ();// sets the width and line spacing of the cy textview www.linuxidc.comLineSpace = linespace;// Build Paint Object Mpaint = new paint (); Mpaint.setantialias (true); Mpaint.setcolor (TextColor); Mpaint.settextsize (TEXTSIZE);switch (typeface) {case 0:mpaint.settypeface ( Typeface.default); Break;case 1:mpaint.settypeface (Typeface.sans_serif); Break;case 2: Mpaint.settypeface (Typeface.serif); Break;case 3:mpaint.settypeface (typeface.monospace); Break;default: Mpaint.settypeface (Typeface.default); break;}} @Overridepublic void setmaxlines (int maxlines) {super.setmaxlines (maxlines);} @Overrideprotected void ondraw (Canvas canvas) {super.ondraw (Canvas);if (m_itextwidth <= 0) {return;} Char ch;int w = 0;int istart = 0;int m_ifontheIGHT;INT&NBSP;M_IREALLINE&NBSP;=&NBSP;0;INT&NBSP;X&NBSP;=&NBSP;DENSITYUTIL.DIP2PX (context, 20); //60 //distance from the left int y = 10; Vector m_string = new vector (); Fontmetrics fm = mpaint.getfontmetrics ();m_ifontheight = (int) Math.ceil ( fm.descent - fm.top) + (int) LineSpace;// calculate font height (font height + line spacing) y = (int) math.ceil (fm.descent - fm.top);for (int i = 0; i < String.Length (); i++) {ch = string.charat (i);float[] widths = new FLOAT[1]; String srt = string.valueof (CH); Mpaint.gettextwidths (srt, widths);if (ch == ' \ n ') {m_irealline++;m_string.addelement (string.substring (istart, i)); istart = i + 1;w = 0;} else {w += (int) (Math.ceil (widths[0));if (w > m_itextwidth) {m_iRealLine++;M_string.addelement (string.substring (istart, i)); istart = i;i--;w = 0;} else {if (i == (String.Length () - 1)) {m_irealline++;m_ String.addelement (String.substring (Istart, string.length ()));}}} /** here is a requirement of 2 lines */if (m_irealline > 2) {M_irealline = 2; }m_itextheight = m_irealline * m_ifontheight + 2;// canvas.setviewport (M_iTextWidth, m_itextwidth);for (int i = 0, j = 0; i < m_ irealline; i++, j++) {canvas.drawtext ((String) (M_string.elementat (i)), x, y + m_ifontheight * j, mpaint);}} @Overrideprotected void onmeasure (Int widthmeasurespec, int heightmeasurespec) { Int measuredwidth = measurewidth (WIDTHMEASURESPEC); m_itextwidth = measuredwidth;/** This is also a requirement */m_itextwidth = m_itextwidTH&NBSP;-&NBSP;DENSITYUTIL.DIP2PX (context, 26); Int measuredheight = measureheight ( HEIGHTMEASURESPEC); this.setmeasureddimension (measuredwidth, measuredheight);// super.onMeasure ( WIDTHMEASURESPEC,&NBSP;HEIGHTMEASURESPEC);} Private int measureheight (Int measurespec) {int specMode = Measurespec.getmode (Measurespec); Int specsize = measurespec.getsize (MeasureSpec);// Default size if no limits are specified.initheight (); int result = m_iTextHeight;if (specmode == measurespec.at_most) {// Calculate the ideal size of your// control within this maximum size.// if your control fills the available// space return the outer Bound.result = specsize;} else if (specmode == measurespec.exactly) {// if your control can fit within these bounds return that Value.result = specsize;} Return result;} Private void initheight () {// set the initial height of cy textview to 0m_itextheight = 0;/ / probably calculates the cy textview required height fontmetrics fm = mpaint.getfontmetrics (); int m_ ifontheight = (int) math.ceil (fm.descent - fm.top) + (int) LineSpace; int line = 0;int istart = 0;int w = 0;for (int i = 0; i < string.length (); i++) {char ch = string.charat (i); float[] widths = new float[1]; String srt = string.valueof (CH); Mpaint.gettextwidths (srt, widths);if (ch == ' \ n ') {line++;istart = i + 1;w = 0;} else {w += (int) (Math.ceil (widths[0));if (w > m_itextwidth) {line++;istart = i;i--;w = 0;} else {if (i == (String.Length () - 1) {line++;}}}} /** here is the demand, requires two lines */if (line > 2) {line = 2; }m_itextheight = (line ) * m_ifontheight + 2;} Private int measurewidth (Int measurespec) {int specMode = Measurespec.getmode (Measurespec); Int specsize = measurespec.getsize (MeasureSpec);// default size if no limits are specified.int result = 500;if (specmode == measurespec.at_most) {// calculate the ideal size of your control// within this maximum size.// If your control fills the available space// return the outer bound.result = Specsize;} else if (Specmode == measurespec.exactly) {// If your control can fit within These bounds return that value.result = specsize;} Return result;} Public void settext (String text) {// Note: This function is currently only called in the UI thread to draw the text out of the other threads <p>// //can't draw text, find for a long time find no reason, seek expert solution) string = text;requestlayout (); invalidate ();}
<com.example.picpopupwindow.CYTextView xmlns:cy= "http://schemas.android.com/apk/ Res-auto " Android:id= "@+id/tv_product_title" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:maxlines= "2" android:paddingleft= "26DP" android:paddingright = "10DP" android:layout_marginbottom= "5DP" android:text= " " cy: Mytextsize= "16SP" />
<declare-styleable name= "Cytextview" > <attr name= "mytextsize" format= "Dimension"/> <attr name= "Mytextcolor" format= "Reference|color"/> <attr name= "Linespacingextra" format= "Dimension" /> <attr name= "typeface" format= "Dimension"/> </declare-styleable>
This is plotted by calling the OnDraw method with the text's wide height. But like Android:paddingleft, android:maxlines and so on these property configuration is not useful, in fact, it is still inconvenient to use.
Why "Android" TextView text typesetting is uneven