android自動換行

來源:互聯網
上載者:User

這個執行個體主要是運用字型的只是,但是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;
}
}
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.