gpt4 book ai didi

android - 如何在 android 自定义 View 的 Canvas 上绘制段落

转载 作者:行者123 更新时间:2023-11-29 00:32:45 30 4
gpt4 key购买 nike

我要在 Canvas 上绘制大量文本。我可以设置文本,但所有文本都显示在一行中。所以我无法阅读所有文本。我想设置文本作为 Canvas 上的一段。谁能告诉我实现此功能的方法。

我尝试使用 getLineCount()、getLineBound() 但它在这些行上提示错误,因为我的 View 不包含这些方法。

最佳答案

您可能必须找到可以容纳在您的 Canvas 宽度内的最长子字符串并重复此操作,直到您的整个“冗长”字符串被分解成更小的字符串。递归在这里有帮助

你可以尝试这样的事情:

class Font
{
private TextPaint tp = new TextPaint();
private Paint p = new Paint();

int getCharWidth(char ch, int fontSize)
{
tp.setTextSize(fontSize);
Rect r = new Rect();
tp.getTextBounds(String.valueOf(ch), 0, 1, r);
return r.width();
}

int getHeight(int fontSize)
{
tp.setTextSize(fontSize);
Rect r = new Rect();
tp.getTextBounds(String.valueOf('A'), 0, 1, r);
return r.height();
}

int getStringWidth(String str, int fontSize)
{
p.setTextSize(fontSize);
tp.setTextSize(fontSize);
Rect r = new Rect();
tp.getTextBounds(str, 0, str.length(), r);
return r.width();//you may also try p.measureText(str);
}
}

public String getLongestSubString(int width, int fontSize, String text)
{
int maxCharPerLine = width / (new Font()).getCharWidth('A', fontSize);
int stringWidth = (new Font()).getStringWidth(text, fontSize);
int posOfSpace = text.lastIndexOf(' ');
if( stringWidth <= width || maxCharPerLine >= text.length())
return text;
String temp = text.substring(0, (posOfSpace == -1) ? (maxCharPerLine > text.length() ? text.length() : maxCharPerLine) : posOfSpace);
return getLongestSubString(width, fontSize, temp);
}

public ArrayList<String> breakTextToLines(int width, int fontSize, String text)
{
if(text == null)
return null;
ArrayList<String> lines = new ArrayList<String>();

text = text.replace("\n", " ");

int length = -1;
String tempText = "";

do
{
if( !tempText.endsWith(" ") && tempText.length() > 0)
length += tempText.length();
else
length += tempText.length() + 1;

if( length >= text.length())
break;

tempText = getLongestSubString(width, fontSize, text.substring(length));
lines.add(tempText);
}
while(length < text.length());

return lines;
}

现在,一旦您将字符串列表(使用 breakTextToLines(int,int,String) 方法)分解为现在可以适合 Canvas 的更小的字符串,您就可以开始您的绘图过程了如下图

int fontSize = 20;
float lineSpaceFactor = 0.5f;
int lineHeight = (new Font()).getHeight(fontSize);
int lineSpace = (int)(lineSpaceFactor * lineHeight);

int width = 256;//your canvas width can go here
int height = 256;//your canvas height can go here
String string = "Need a lengthy string? why not Dr Zoidberg maybe? woop woop woop!!!";//your lengthy string can go here
Canvas canvas = new Canvas(bitmap);//your canvas or bitmap source goes here
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setTextAlign(Align.CENTER);
paint.setTextSize(fontSize);

ArrayList<String> lines = breakTextToLines(width, fontSize, string);
int numOfLines = lines.size();
int overallHeight = numOfLines * (lineHeight + lineSpace);

int yLoc = (height - overallHeight) / 2;
yLoc += lineSpace;

for(String line : lines)
{
int xLoc = textureWidth /2 ;
canvas.drawText(text, xLoc, yLoc, paint);

yLoc += (lineHeight + lineSpace);
}
//your canvas would now be drawn with the paragraph broken into lines and centre aligned

希望这对您有所帮助。

关于android - 如何在 android 自定义 View 的 Canvas 上绘制段落,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14394263/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com