如何识别英文单词java,自动识别英文单词显示(Android+Java)

应用:

在做txt阅读器或者显示英文字符时,要判断一个单词是否被断开啦。如下简单代码能够自动识别单词,并且绘画在屏幕上。

貌似目前很多阅读器,比如百度文库和百阅,都没有对单词处理。看着相当蛋疼。不过你处理来单词后,后面将有很多内容

需要处理。这就是百度文库(android客户端)或者百阅阅读器舍弃对文字处理的原因。

package com.docintes.canvas;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Path;

import android.graphics.Rect;

import android.util.AttributeSet;

import android.util.Log;

import android.view.View;

public class MyTextView extends View {

private Paint mPaint;

private static String mString = "Whatonship of deep love, about love? In a relationship of deep love,In a relationship of deep love, all the sharing that we discussed above are taken for granted. But love transcends all this. During love, we are attached with a particular person, while in friendship, one may have many friends. A loving relationship makes one so much attached to the other, that one gets pained if his/her beloved is hurt! Love also involves a physical element. Friendship does not have that. This is a vital difference. Nature gives us love so that the specie can go forward. Nature does not give us friendship.";

private Path mPath;

private int textSize = 24;

private static int xAdd = 0;

private float strheight = 0;

private float strwidth = 0;

final int strLength=mString.length();

public MyTextView(Context context) {

super(context);

}

public MyTextView(Context context, AttributeSet attr) {

super(context, attr);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

mPaint = new Paint();

mPath = new Path();

mPaint.setColor(Color.argb(255, 255, 255, 255)); // mPaint设置画布颜色

canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint); // 画一个长方形用于显示歌词

mPaint.setColor(Color.argb(255, 0, 0, 0));

mPath.moveTo(0, 0);

mPath.lineTo(getWidth(), 0);

Rect rec = new Rect();

mPaint.setTextSize(textSize);

mPaint.setAntiAlias(true);

mPaint.getTextBounds(mString, 0, mString.length(), rec);

strheight = rec.height();

strwidth = mPaint.measureText(mString);

while (strwidth >= getWidth())

{

strwidth = drawTheText(canvas, mString,mPath, mPaint);

}

//canvas.drawTextOnPath(mString, mPath, xAdd, height, mPaint);

}

static float height = 0;

float drawTheText(Canvas canvas, String str, Path mPath,Paint mPaint)

{

int theLinePoint = 0;

StringBuffer tempString =new StringBuffer();

float tempwidth = 0;

do

{

tempString.append(str.charAt(theLinePoint));

tempwidth = mPaint.measureText(tempString.toString());

theLinePoint ++;

}while((tempwidth +mPaint.measureText("A"))<= getWidth()&&theLinePoint

height += (strheight + 10);

if(str.charAt(theLinePoint-1)!=' '&&theLinePoint

{

theLinePoint--;

do{

if(str.charAt(theLinePoint)!=' ')

theLinePoint--;

else

{

break;

}

}while(theLinePoint>1);

canvas.drawTextOnPath(str.substring(0,theLinePoint), mPath, xAdd, height, mPaint);

}

else {

canvas.drawTextOnPath(str.substring(0,theLinePoint), mPath, xAdd, height, mPaint);

}

mString=str.substring(theLinePoint).trim();

Log.i("............wokou",mString);

return mPaint.measureText(str);

}

}

效果图:

0_1311042149gbma.gif

( 显示的字是一个单词,注意换行的地方。)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值