Category:Android UI
Article From:https://www.cnblogs.com/ganchuanpu/p/6607546.html

Recently made contacts, small screen machine contacts name display – length over the border font smaller.

 * Custom TextView, text content automatically adjust the font size to adapt to the size of TextView.@author YZPItPublic class AutoFitTextView extends TExtViewPrivate Paint mTextPaint;Private float mTextSize;SuchPublic AutoFitTextView (Context context)Super (context);    }  SuchPublic AutoFitTextView (Context CO)Ntext, AttributeSet attrs)Super (context, attrs);    }  SuchA / * *Re size tHe, font, so, the, specified, text, fits, in, the, text, text, box, and text.Text box is the specified wiDth.""@param text@param textWidth""Private void refitText (Str)Ing text, int textViewWidth)If text (text = = null null textViewWidth = <)ReTurnMTextPaint = new Paint ();MTextPaint.set (this.getPaint ())Int aVailableTextViewWidth = = getWidth () getPaddingLeft - (-) - getPaddingRight ();Float[] charsWiDthArr = new float[text.length ()]Rect boundsRect new = RectMTextPaint.getTExtBounds (text, 0, text.length (), boundsRect)Int (textWidth) = boundsRect.width ();     MTextSize = getTextSize ();While textWidth (> availableTextViewWidth)""MTextSize = = 1;MTextPaint.setTextSize (mTextSize);TextWidth, mTextPaInt.getTextWidths (text, charsWidthArr);""This.setTextSize (TypedValue.COMPLEX_UN)IT_PX, mTextSize);    }  Such@OverrideProtected void onDraw (Canvas canvas)""Super.onDraw (canvas);RefitText (this.getText ().ToString (), this.getWidth ())    }  } 


Leave a Reply

Your email address will not be published. Required fields are marked *