这个实例主要是运用字体的只是,但是android自带就几种,虽然android有提供可以自己转载字体的功能,但我试了下,没发现成功的,所以我代码里面就没有弄了,就弄了自带的。明天再试试效果,发现书本的代码不怎么适合android,当字体变大时,会出现按两次才能换行的bug。以后我会修订这个bug
下面是效果图:
mainView的代码:
Java代码
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; //显示宽度
public Typeface m_nTypeFace = null; //存储字体
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();
}
}
}
//开始游戏主线程
public void start()
{
if(gameThread == null)
{
gameThread = new Thread(this);
gameThread.start();
}
}
//停止游戏主线程
public void stop()
{
isGame = false;
if(gameThread != null)
{
try {
gameThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//输入判断
public void input()
{
if( keyCode == KeyEvent.KEYCODE_DPAD_UP && isKeyDown)
{
m_nWidth = m_nWidth +10;
isKeyDown = false;
}
//如果按下方向键的下键,则调整当前行的位置
else if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN && isKeyDown)
{
m_nWidth = m_nWidth -10;
isKeyDown = false;
}
}
//逻辑判断
public void logic()
{
}
public void doDraw()
{
Canvas c = null;
try
{
c = holder.lockCanvas();
synchronized (holder) {
paint(c);
}
}finally{
if(c != null)
{
holder.unlockCanvasAndPost(c);
}
}
}
//画图
public void paint(Canvas canvas)
{
//清贫
canvas.drawRect(0, 0, getWidth(), getHeight(),backPaint);
TextAjust.AjustDrawString(canvas,forePaint,
"我要自动换行我要自动换行我要自动换行我要自动换行",
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
//如果按下方向键的上键,则调整当前行的位置
this.keyCode = keyCode;
isKeyDown = true;
return true;
}
}
书上提供的换行功能源码,我把画图的改成android的,其他的没改,但代码效果不好
下面给出代码
TextAjust.java
Java代码 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; //需要换行的位置
while (true)
{
//获得句子的断点位置
nPos = ChangLine(strText, paint, linewd );
if (nPos == 0)
{
//如果nPos为0,代表输入的字符串为空
canvas.drawText( strText, x, y,paint);
break;
}
else
{
//判断是否最后一句
if (strText.charAt(nPos - 1) == '\n' )
subStr = strText.substring(0, nPos - 1);
else
subStr = strText.substring(0, nPos);
canvas.drawText( subStr, x, y, paint);
//截断句子,继续进行处理
strText = strText.substring(nPos, strText.length());
y = y + yDis;
}
}
}
}