This article describes the Android rewrite TextView to achieve a neat text typesetting method. Share to everyone for your reference, specific as follows:
Xrtextview class
Package rong.android.test;
Import Org.json.JSONArray;
Import org.json.JSONException;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.util.AttributeSet;
Import Android.view.View;
Import Android.widget.TextView;
public class Xrtextview extends textview{private final String namespace = "Rong.android.TextView";
private String text;
private float textsize;
private float paddingleft;
private float paddingright;
private float marginleft;
private float marginright;
private int textcolor;
Private Jsonarray ColorIndex;
Private Paint paint1 = new Paint ();
Private Paint Paintcolor = new Paint ();
private float textshowwidth;
private float Spacing = 0; private float linespacing = 1.3f;//between line and line public xrtextview (context, AttributeSet attrs) {Super (context, attrs)
;
Text = Attrs.getattributevalue ("http://schemas.android.com/apk/res/android", "text"); Textsize = Attrs.getattributeIntvalue (namespace, "Textsize", 25)//font Size TextColor = Attrs.getattributeintvalue (namespace, "TextColor", Color.Blue)
;//FONT Color paddingleft = Attrs.getattributeintvalue (namespace, "Paddingleft", 0);
Paddingright = Attrs.getattributeintvalue (namespace, "Paddingright", 0);
MarginLeft = Attrs.getattributeintvalue (namespace, "MarginLeft", 0);
MarginRight = Attrs.getattributeintvalue (namespace, "MarginRight", 0);
Paint1.settextsize (TEXTSIZE);
Paint1.setcolor (TextColor);
Paint1.setantialias (TRUE);
Paintcolor.setantialias (TRUE);
Paintcolor.settextsize (TEXTSIZE);
Paintcolor.setcolor (Color.Blue); Public Xrtextview (context context, float textsize, int textcolor, float paddingleft, float paddingright, float Marginle
FT, float marginright) {super (context);
This.textsize = Textsize;
This.textcolor = TextColor;
This.paddingleft = paddingleft;
This.paddingright = paddingright;
This.marginleft = MarginLeft;
This.marginright = marginright;
Paint1.settextsize (TEXTSIZE); Paint1.setcoloR (TextColor);
Paint1.setantialias (TRUE);
Paintcolor.setantialias (TRUE);
Paintcolor.settextsize (TEXTSIZE);
Paintcolor.setcolor (Color.Blue);
Public Jsonarray Getcolorindex () {return colorindex;
public void Setcolorindex (Jsonarray colorindex) {this.colorindex = ColorIndex; /** * Incoming an index to determine whether the current word is highlighted * @param index * @return * @throws jsonexception/public boolean iscolor (int index) th
Rows jsonexception{if (ColorIndex = = null) {return false;
for (int i = 0; i < colorindex.length (); i + +) {Jsonarray array = Colorindex.getjsonarray (i);
int start = array.getint (0);
int end = Array.getint (1)-1;
if (index >= start && index <= end) {return true;
return false;
} @Override protected void OnDraw (Canvas Canvas) {//Super.ondraw (Canvas);
View view= (view) this.getparent ();
Textshowwidth=view.getmeasuredwidth ()-paddingleft-paddingright-marginleft-marginright;
int linecount = 0; Text = This.gettext (). toString ();//.replaceall ("\ n", "\ r \ n");
if (text==null) return;
char[] Textchararray = Text.tochararray ();
Painted width float drawedwidth = 0;
float Charwidth;
for (int i = 0; i < textchararray.length i++) {charwidth = Paint1.measuretext (Textchararray, I, 1);
if (textchararray[i]== ' \ n ') {linecount++;
drawedwidth = 0;
Continue
} if (Textshowwidth-drawedwidth < charwidth) {linecount++;
drawedwidth = 0;
Boolean color = false;
try {color = IsColor (i);
catch (Jsonexception E1) {//TODO auto-generated catch block E1.printstacktrace (); } if (color) {Canvas.drawtext (Textchararray, I, 1, paddingleft + drawedwidth, (LineCount + 1) * Textsize * Linespaci
Ng, Paintcolor); }else{Canvas.drawtext (Textchararray, I, 1, paddingleft + drawedwidth, (LineCount + 1) * Textsize * linespacing, pain
T1); } if (Textchararray[i] > 127 && textchararray[i]!= ', ' && textchararray[i '!= ', ' && Textchar Array[i]!= '. ' && Textchararray[i]!= ': ' && textchararray[i]!= '!
') {drawedwidth + = charwidth + Spacing;
}else{Drawedwidth + = Charwidth;
} setheight ((int) (LineCount + 1) * (int) textsize * linespacing + 10));
public float getspacing () {return Spacing;
public void setspacing (float spacing) {spacing = spacing;
public float getmylinespacing () {return linespacing;
public void setmylinespacing (float linespacing) {linespacing = linespacing;
public float getmytextsize () {return textsize;
public void Setmytextsize (float textsize) {this.textsize = Textsize;
Paint1.settextsize (TEXTSIZE);
Paintcolor.settextsize (TEXTSIZE);
}
}
Mainactivity class
Package rong.android.test;
Import Android.os.Bundle;
Import Android.widget.TextView;
Import android.app.Activity;
public class Mainactivity extends activity {
private xrtextview Xrtextview = null;
Private TextView TextView = null;
Private String content = "Abcdefgabcdef I want you to LFWJKDFL;SKJF Asljkflskjfls; KJFSLJFWFISDLFJSLLKJSDFJLSKJF546132S1F3SD4F31S3DFFSLFKSJDFLJLSADKJFLSAJDF Sdfjklsajdflsa;jdfls's!@#$%^&* () _ " ;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
Xrtextview = (Xrtextview) This.findviewbyid (R.ID.MYTEXTVIEW_TV);
Xrtextview.settext (content);
TextView = (TextView) This.findviewbyid (R.ID.MYTEXTVIEW_TV1);
Textview.settext (content);
}
Layout file
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http:// Schemas.android.com/tools "
android:layout_width=" match_parent "
android:layout_height=" Match_parent "
android:orientation= "vertical" >
<rong.android.test.xrtextview
android:id= "@+id/mytextview_ TV "
android:layout_width=" match_parent "
android:layout_height=" wrap_content "/>
<textview
android:id= "@+id/mytextview_tv1"
android:layout_width= "match_parent"
android:layout_height= " Wrap_content "
android:textcolor=" @android: Color/black "/>
</LinearLayout>
Full instance code click here to download the site.
More interested readers of Android-related content can view this site: "Introduction to Android Development and advanced Course", "Android Service Component Usage Summary", "Android Basic component Usage Summary" and "Android Control usage Summary"
I hope this article will help you with the Android program.