自动识别英文单词显示(Android+Java)

来源:互联网 发布:地理探测器软件下载 编辑:程序博客网 时间:2024/05/01 18:46
应用:    在做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);}@Overrideprotected 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<str.length());height += (strheight + 10);if(str.charAt(theLinePoint-1)!=' '&&theLinePoint<str.length()){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);}}


效果图:

          

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

原创粉丝点击