Fixed the textview size and adjusted the font size according to the number of texts.

Source: Internet
Author: User

/**
* 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.

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.