/**
* File name autoresizetextview. Java
* Contains the Class Name List com. Haier. Internet. conditioner. haierinternetconditioner2.view
* Version number
* Creation date: January 1, September 9, 2014
* Copyright notice
*/
Package com. Haier. Internet. conditioner. haierinternetconditioner2.view;
/**
* Class Name
* @ Author Wang Honghe <br/>
* Main functions.
* Creation date: January 1, September 9, 2014
*/
Import Android. content. context;
Import Android. Graphics. Canvas;
Import Android. Text. layout. alignment;
Import Android. Text. staticlayout;
Import Android. Text. textpaint;
Import Android. Text. textutils;
Import Android. util. attributeset;
Import Android. util. typedvalue;
Import Android. widget. textview;
Public class autoresizetextview extends textview {
Private Static final int max_size = 1000;
Private Static final int min_size = 5;
Private textpaint mtextpaint;
Private float mspacingmult = 1.0f;
Private float mspacingadd = 0.0f;
Private Boolean needadapt = false;
Private Boolean adapting = false;
Public autoresizetextview (context, attributeset attrs, int defstyle ){
Super (context, attrs, defstyle );
Init ();
}
Public autoresizetextview (context, attributeset attrs ){
Super (context, attrs );
Init ();
}
Public autoresizetextview (context ){
Super (context );
Init ();
}
Private void Init (){
Mtextpaint = new textpaint ();
}
@ Override
Protected void ondraw (canvas ){
If (adapting ){
Return;
}
If (needadapt ){
Adapttextsize ();
} Else {
Super. ondraw (canvas );
}
}
Private void adapttextsize (){
Charsequence text = gettext ();
Int viewwidth = getmeasuredwidth ();
Int viewheight = getmeasuredheight ();
If (viewwidth = 0 | viewheight = 0 | textutils. isempty (text )){
Return;
}
Adapting = true;
/* Binary Search */
Int Bottom = min_size, Top = max_size, mid = 0;
While (bottom <= Top ){
Mid = (bottom + TOP)/2;
Mtextpaint. settextsize (MID );
Int textwidth = (INT) mtextpaint. measuretext (text, 0, text. Length ());
Int textheight = gettextheight (text, viewwidth );
If (textwidth <viewwidth & textheight <viewheight ){
Bottom = Mid + 1;
} Else {
Top = mid-1;
}
}
Int newsize = mid-1;
Settextsize (typedvalue. complex_unit_px, newsize );
Adapting = false;
Needadapt = false;
Invalidate ();
}
Private int gettextheight (charsequence text, int targetwidth ){
Staticlayout layout = new staticlayout (text, mtextpaint, targetwidth,
Alignment. align_normal, mspacingmult, mspacingadd, true );
Return layout. getheight ();
}
@ Override
Protected void onsizechanged (int w, int H, int oldw, int oldh ){
Super. onsizechanged (W, H, oldw, oldh );
Needadapt = true;
}
@ Override
Protected void ontextchanged (charsequence text, int start, int lengthbefore, int lengthafter ){
Super. ontextchanged (text, start, lengthbefore, lengthafter );
Needadapt = true;
}
@ Override
Public void setlinespacing (float add, float mult ){
Super. setlinespacing (ADD, mult );
Mspacingmult = mult;
Mspacingadd = add;
}
}
There are many methods, you can refer to the http://stackoverflow.com/questions/5033012/auto-scale-textview-text-to-fit-within-bounds
Fixed the textview size and adjusted the font size according to the number of texts.