String Tool class:
/**
* Text word wrap is implemented.
* Automatic Paging
*/
Package com.yarin.android.Examples_05_06;
Import Java.util.Vector;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.graphics.Paint.FontMetrics;
Import android.view.KeyEvent;
public class Textutil
{
X points drawn by intm_itextposx;//
Y points drawn by intm_itextposy;//
intm_itextwidth;//Drawing width
intm_itextheight;//Drawing Height
intm_ifontheight;//font Height
intm_ipagelinenum;//the number of rows displayed per page
Intm_itextbgcolor; Background color
intm_itextcolor;//Font Color
Intm_ialpha;//alpha value
intm_irealline;//string true number of rows
intm_icurline;//as a forward
Stringm_strtext;
vectorm_string;
Paintm_paint;
Intm_itextsize;
Public Textutil ()
{
M_paint = new paint ();
m_string = new Vector ();
}
Public Textutil (String strText, int x, int y, int w, int h, int bgcolor, int txetcolor, int a, int itextsize)
{
M_paint = new paint ();
m_string = new Vector ();
M_strtext = StrText;
M_ITEXTPOSX = x;
M_itextposy = y;
M_itextwidth = W;
M_itextheight = h;
M_itextbgcolor = bgcolor;
M_itextcolor = Txetcolor;
M_itextsize = itextsize;
M_ialpha = A;
}
/**
* Initialization
* @param strtext the string to display
* @param xx
* @param yy
* @param ww
* @param hh
* @param bgcolor background color
* @param the color of txetcolor text
* @param aalpha
* @param itextsize Font size
*/
public void Inittext (String strText, int x, int y, int w, int h, int bgcolor, int txetcolor, int a, int itextsize)
{
M_icurline = 0;
M_ipagelinenum = 0;
M_irealline = 0;
M_strtext = "";
M_ITEXTPOSX = 0;
M_itextposy = 0;
m_itextwidth = 0;
m_itextheight = 0;
M_itextbgcolor = 0;
M_itextcolor = 0;
m_itextsize = 0;
M_ialpha = 0;
M_string.clear ();
SetText (StrText);
SetRect (x, Y, W, h);
Setbgcolor (bgcolor);
SetTextColor (Txetcolor);
Setfontsize (itextsize);
Setalpha (a);
Setpaint ();
Gettextifon ();
}
/**
* Set Alpha
* @param aalpha value
*/
public void Setalpha (int a)
{
M_ialpha = A;
}
/**
* Settings for the Paint property
*/
public void Setpaint ()
{
M_paint.setargb (M_ialpha, Color.Red (M_itextcolor), Color.green (M_itextcolor), Color.Blue (M_iTextColor));
M_paint.settextsize (m_itextsize);
}
/**
* Set Font size
* @param itextsize
*/
public void setfontsize (int itextsize)
{
M_itextsize = itextsize;
}
/**
* Set the area where the text is displayed
* @param x
* @param y
* @param W
* @param h
*/
public void SetRect (int x, int y, int w, int h)
{
M_ITEXTPOSX = x;
M_itextposy = y;
M_itextwidth = W;
M_itextheight = h;
}
/**
* Set Background color
* @param bgcolor
*/
public void Setbgcolor (int bgcolor)
{
M_itextbgcolor = bgcolor;
}
/**
* Set Font Color
* @param txetcolor
*/
public void SetTextColor (int txetcolor)
{
M_itextcolor = Txetcolor;
}
/**
* Color Draw the string to display
* @param strText
*/
public void SetText (String strText)
{
M_strtext = StrText;
}
/**
* Get the information of the string
* Includes: Number of lines, pages and other information
* Internal Call
*/
public void Gettextifon ()
{
Char ch;
int w = 0;
int istart = 0;
FontMetrics fm = M_paint.getfontmetrics ();
M_ifontheight = (int) Math.ceil (fm.descent-fm.top) + 2;
M_ipagelinenum = M_itextheight/m_ifontheight;
for (int i = 0; i < m_strtext.length (); i++)
{
ch = m_strtext.charat (i);
Float[] widths = new float[1];
String srt = string.valueof (CH);
M_paint.gettextwidths (srt, widths);
if (ch = = ' \ n ')
{
m_irealline++;
M_string.addelement (m_strtext.substring (Istart, i));
Istart = i + 1;
w = 0;
}
Else
{
W + = (int) (Math.ceil (widths[0]));
if (W > M_itextwidth)
{
m_irealline++;
M_string.addelement (m_strtext.substring (Istart, i));
Istart = i;
i--;
w = 0;
}
Else
{
if (i = = (M_strtext.length ()-1))
{
m_irealline++;
M_string.addelement (M_strtext.substring (Istart, M_strtext.length ()));
}
}
}
}
}
/**
* Draw a String
* @param canvas
*/
public void DrawText (canvas canvas)
{
for (int i = m_icurline, j = 0; i < M_irealline; i++, J + +)
{
if (J > M_ipagelinenum)
{
Break
}
Canvas.drawtext (String) (M_string.elementat (i)), m_itextposx, M_itextposy + m_ifontheight * j, M_paint);
}
}
/**
* Paging and other key processing
* @param keycode
* @param Event
* @return
*/
public boolean KeyDown (int keycode, keyevent event)
{
if (keycode = = keyevent.keycode_dpad_up)
{
if (M_icurline > 0)
{
m_icurline--;
}
}
else if (keycode = = Keyevent.keycode_dpad_down)
{
if ((M_icurline + M_ipagelinenum) < (m_irealline-1))
{
m_icurline++;
}
}
return false;
}
}
View class:
Package com.yarin.android.Examples_05_06;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import android.view.KeyEvent;
Import android.view.MotionEvent;
Import Android.view.View;
public class Gameview extends View implements Runnable
{
/* Declare Paint Object */
Private Paint mpaint = null;
private int micount = 0;
/* Declare textutil Object */
Private Textutil mtextutil = null;
Public Gameview (Context context)
{
Super (context);
/* Build Object */
Mpaint = new Paint ();
String string = "Test wrap word \ n" Set text wrap Abcdefgh\niklmnopqrst line wrap 123347465\n43756245android\n set text wrap abcdefgh\ Niklmnopqrst line 123347465\n43756245android ";
/* Instantiate Textutil */
Mtextutil = new Textutil (string,5,50,300,80,0x0,0xffffff,255,16);
/* Initialize Textutil */
Mtextutil.inittext (string,5,50,300,80,0x0,0xffffff,255,16);
/* Open Thread */
New Thread (This). Start ();
}
public void OnDraw (canvas canvas)
{
Super.ondraw (canvas);
/* Set Background color */
Canvas.drawcolor (Color.Black);
Mpaint.setantialias (TRUE);
if (Micount < 100)
{
micount++;
}
Mpaint.setcolor (color.red);
Canvas.drawtext ("Installed in progress:" +micount+ "% ...", ten, A, mpaint);
/* Draw Textutil: Implement auto-wrap */
Mtextutil.drawtext (canvas);
}
Stylus Events
public boolean ontouchevent (Motionevent event)
{
return true;
}
Press the event by pressing the key
public boolean onKeyDown (int keycode, keyevent event)
{
Return Mtextutil.keydown (KeyCode, event);
}
Press the key to bounce the event
public boolean onKeyUp (int keycode, keyevent event)
{
return false;
}
public boolean onkeymultiple (int keycode, int repeatcount, keyevent event)
{
return true;
}
/**
* Thread Handling
*/
public void Run ()
{
while (! Thread.CurrentThread (). isinterrupted ())
{
Try
{
Thread.Sleep (100);
}
catch (Interruptedexception e)
{
Thread.CurrentThread (). interrupt ();
}
Use Postinvalidate to update the interface directly in the thread
Postinvalidate ();
}
}
}
To draw a string: