Android line feed

Source: Internet
Author: User

This example mainly uses the font, but android comes with only a few types. Although android provides the font reprinting function, I tried it and did not find it successful, so I didn't get it in the code, so I got it. Try again tomorrow and find that the book code is not very suitable for android. When the font size increases, there will be a bug that only two lines can be entered. I will revise this bug later

Below is:

MainView code:

Java code

Package com. wjh. demon_5;

Import android. content. Context;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. graphics. Typeface;
Import android. view. KeyEvent;
Import android. view. SurfaceHolder;
Import android. view. SurfaceView;
Import android. view. SurfaceHolder. Callback;

Public class MainView extends SurfaceView implements Callback, Runnable {
Thread gameThread = null;
Boolean isGame = true;
SurfaceHolder holder = null;
Paint forePaint = null;
Paint backPaint = null;

Int keyCode =-1;
Boolean isKeyDown = false;

Public int m_nWidth = 50; // display width
Public Typeface m_nTypeFace = null; // store the font
Public MainView (Context context ){
Super (context );
// TODO Auto-generated constructor stub
SetFocusable (true );
GetHolder (). addCallback (this );
Holder = this. getHolder ();
BackPaint = new Paint ();
BackPaint. setColor (Color. BLACK );

ForePaint = new Paint ();
ForePaint. setTypeface (Typeface. DEFAULT_BOLD );
ForePaint. setTextSize (20 );
ForePaint. setColor (Color. RED );

}
@ Override
Public void run (){
// TODO Auto-generated method stub
While (isGame)
{
Input ();
Logic ();
DoDraw ();
Try {
Thread. sleep (100 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}

}
// Start the main game thread
Public void start ()
{
If (gameThread = null)
{
GameThread = new Thread (this );
GameThread. start ();
}
}
// Stop the main game thread
Public void stop ()
{
IsGame = false;
If (gameThread! = Null)
{
Try {
GameThread. join ();
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
// Input judgment
Public void input ()
{
If (keyCode = KeyEvent. KEYCODE_DPAD_UP & isKeyDown)
{
M_nWidth = m_nWidth + 10;
IsKeyDown = false;
}
// Adjust the position of the current row if you press the arrow key.
Else if (keyCode = KeyEvent. KEYCODE_DPAD_DOWN & isKeyDown)
{
M_nWidth = m_nWidth-10;
IsKeyDown = false;
}

}
// Logical judgment
Public void logic ()
{

}
Public void doDraw ()
{
Canvas c = null;
Try
{
C = holder. lockCanvas ();
Synchronized (holder ){
Paint (c );
}
} Finally {
If (c! = Null)
{
Holder. unlockCanvasAndPost (c );
}
}
}
// Draw
Public void paint (Canvas canvas)
{
// Poverty alleviation
Canvas. drawRect (0, 0, getWidth (), getHeight (), backPaint );

TextAjust. AjustDrawString (canvas, forePaint,
"I want to wrap automatically ",
M_nWidth, 50, 50, 30 );

}
@ Override
Public void surfaceChanged (SurfaceHolder holder, int format, int width,
Int height ){
// TODO Auto-generated method stub

}
@ Override
Public void surfaceCreated (SurfaceHolder holder ){
// TODO Auto-generated method stub
Start ();

}
@ Override
Public void surfaceDestroyed (SurfaceHolder holder ){
// TODO Auto-generated method stub
Stop ();

}
@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
// TODO Auto-generated method stub
// If you press the arrow key, adjust the position of the current row.
This. keyCode = keyCode;
IsKeyDown = true;

Return true;
}

}

The source code of the line feed function provided in the book, I changed the drawing to android, but nothing else was changed, but the Code did not work well.

The following code is provided:

TextAjust. java

Java code package com. wjh. demon_5;

Import android. graphics. Canvas;
Import android. graphics. Paint;

Public class TextAjust {
Static public int ChangLine (String str, Paint paint, int linewd)
{
Int wd = 0;
Char ch;
For (int I = 0; I <str. length (); I ++)
{
Ch = str. charAt (I );
If (ch = '\ n ')
Return I + 1;

Wd + = paint. getTextSize ();
If (wd> linewd)
Return I;
}
Return 0;
}

Static public void AjustDrawString (Canvas canvas, Paint paint, String strText,
Int linewd, int x, int y, int yDis)
{
String subStr;
Int nPos; // the position where the line feed is needed.
While (true)
{
// Obtain the breakpoint position of a sentence
NPos = ChangLine (strText, paint, linewd );
If (nPos = 0)
{
// If nPos is 0, the input string is null.
Canvas. drawText (strText, x, y, paint );
Break;
}
Else
{
// Determine whether the last sentence is used
If (strText. charAt (nPos-1) = '\ n ')
SubStr = strText. substring (0, nPos-1 );
Else
SubStr = strText. substring (0, nPos );
Canvas. drawText (subStr, x, y, paint );
// Cut the sentence and continue processing
StrText = strText. substring (nPos, strText. length ());
Y = y + yDis;
}
}
}
}

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.