tools.java
来自「Sony Ericsson手机上的Facebook客户端全套代码」· Java 代码 · 共 683 行 · 第 1/2 页
JAVA
683 行
}
return null;
}
public static String highlightWords(String src, String words[], String color)
{
String startTag = "<font color=\"" + color + "\">";
String endTag = "</font>";
int wordLength = words.length;
label0:
for(int i = 0; i < wordLength; i++)
{
int wordPos = src.indexOf(words[i]);
do
{
if(wordPos == -1)
continue label0;
if(wordPos <= 0 || src.charAt(wordPos - 1) != '>')
break;
wordPos = src.indexOf(words[i], wordPos + 1);
} while(true);
src = src.substring(0, wordPos) + startTag + words[i] + endTag + src.substring(wordPos + words[i].length());
}
return src;
}
public static final Vector splitStringByWidth(String src, int maxWidth, Font font, int color, int truncation, boolean handleHTML, int maxLines)
{
int totalNrChars;
int textOffset;
int xPos;
int yPos;
int lineNr;
String link;
TextStyle style;
Vector styleStack;
Vector parts;
boolean done;
if(src == null || src.length() == 0)
return null;
totalNrChars = src.length();
textOffset = 0;
xPos = 0;
yPos = 0;
lineNr = 0;
link = null;
style = new TextStyle();
styleStack = new Vector();
parts = new Vector();
style.font = font;
style.color = color;
done = false;
_L4:
int charsBeforeTag;
int ellipsisWidth;
int nrPartChars;
TextPart part;
if(done || textOffset >= totalNrChars || maxLines != -1 && lineNr >= maxLines)
break; /* Loop/switch isn't completed */
boolean lastLine = maxLines != -1 && lineNr == maxLines - 1;
int nextPos;
if(handleHTML)
nextPos = firstIndexOfAnyChar(src, "<\r\n", textOffset);
else
nextPos = firstIndexOfAnyChar(src, "\r\n", textOffset);
if(nextPos < 0)
charsBeforeTag = totalNrChars - textOffset;
else
charsBeforeTag = nextPos - textOffset;
if(charsBeforeTag == 0)
{
int charsLeft = totalNrChars - textOffset;
if(src.charAt(textOffset) == '\n' || handleHTML && charsLeft >= 3 && src.substring(textOffset, textOffset + 3).compareTo("<br") == 0)
{
lineNr++;
yPos += style.font.getHeight();
xPos = 0;
if(src.charAt(textOffset) == '\n')
{
textOffset++;
} else
{
nextPos = src.indexOf(62, textOffset + 3);
if(nextPos < 0)
done = true;
else
textOffset = nextPos + 1;
}
} else
if(src.charAt(textOffset) == '\r')
{
lineNr++;
yPos += style.font.getHeight();
xPos = 0;
if(++textOffset < totalNrChars && src.charAt(textOffset) == '\n')
textOffset++;
} else
if(handleHTML)
if(charsLeft >= 2 && src.substring(textOffset, textOffset + 2).compareTo("</") == 0)
{
if(!styleStack.isEmpty())
{
style = (TextStyle)styleStack.lastElement();
styleStack.removeElementAt(styleStack.size() - 1);
}
nextPos = src.indexOf(62, textOffset + 2);
if(nextPos < 0)
{
done = true;
} else
{
if(src.charAt(textOffset + 2) == 'a')
link = null;
textOffset = nextPos + 1;
}
} else
if(charsLeft >= 3 && src.substring(textOffset, textOffset + 3).compareTo("<b>") == 0)
{
TextStyle prevStyle = style;
styleStack.addElement(prevStyle);
style = new TextStyle();
style.color = prevStyle.color;
style.font = Font.getFont(prevStyle.font.getFace(), prevStyle.font.getStyle() | 0x1, prevStyle.font.getSize());
textOffset += 3;
} else
if(charsLeft >= 3 && src.substring(textOffset, textOffset + 3).compareTo("<i>") == 0)
{
TextStyle prevStyle = style;
styleStack.addElement(prevStyle);
style = new TextStyle();
style.color = prevStyle.color;
style.font = Font.getFont(prevStyle.font.getFace(), prevStyle.font.getStyle() | 0x2, prevStyle.font.getSize());
textOffset += 3;
} else
if(charsLeft >= 3 && src.substring(textOffset, textOffset + 3).compareTo("<u>") == 0)
{
TextStyle prevStyle = style;
styleStack.addElement(prevStyle);
style = new TextStyle();
style.color = prevStyle.color;
style.font = Font.getFont(prevStyle.font.getFace(), prevStyle.font.getStyle() | 0x4, prevStyle.font.getSize());
textOffset += 3;
} else
if(charsLeft >= 3 && src.substring(textOffset, textOffset + 3).compareTo("<a ") == 0)
{
nextPos = src.indexOf(62, textOffset + 3);
if(nextPos < 0)
{
done = true;
} else
{
link = parseLinkParam(src, textOffset + 3, nextPos);
textOffset = nextPos + 1;
}
} else
if(charsLeft >= 5 && src.substring(textOffset, textOffset + 5).compareTo("<font") == 0)
{
nextPos = src.indexOf(62, textOffset + 5);
if(nextPos < 0)
{
done = true;
} else
{
TextStyle prevStyle = style;
styleStack.addElement(prevStyle);
style = new TextStyle();
style.color = prevStyle.color;
style.font = prevStyle.font;
parseFontParam(src, textOffset + 5, nextPos, style);
textOffset = nextPos + 1;
}
} else
{
done = true;
}
continue; /* Loop/switch isn't completed */
}
boolean breakOnWord = !lastLine || truncation == 2;
ellipsisWidth = 0;
if(lastLine && truncation != 0)
ellipsisWidth = style.font.stringWidth("...");
int maxPartWidth = maxWidth != -1 ? maxWidth - xPos : -1;
boolean foundWordBreak = false;
nrPartChars = 0;
if(maxPartWidth == -1)
{
nrPartChars = charsBeforeTag;
} else
{
for(; nrPartChars < charsBeforeTag && style.font.substringWidth(src, textOffset, nrPartChars) < maxPartWidth; nrPartChars++);
if(nrPartChars < charsBeforeTag && ellipsisWidth > 0)
for(; nrPartChars > 0 && style.font.substringWidth(src, textOffset, nrPartChars) + ellipsisWidth > maxPartWidth; nrPartChars--);
if(nrPartChars < charsBeforeTag && breakOnWord)
{
int i = nrPartChars - 1;
do
{
if(i < 0)
break;
if(src.charAt(textOffset + i) == ' ')
{
nrPartChars = i;
foundWordBreak = true;
break;
}
i--;
} while(true);
}
}
if(nrPartChars <= 0)
{
if(xPos <= 0)
{
done = true;
} else
{
for(; textOffset < totalNrChars && src.charAt(textOffset) == ' '; textOffset++);
lineNr++;
yPos += style.font.getHeight();
xPos = 0;
}
continue; /* Loop/switch isn't completed */
}
if(nrPartChars < charsBeforeTag && breakOnWord && !foundWordBreak && xPos > 0)
{
if(lastLine)
{
done = true;
if(ellipsisWidth > 0 && maxPartWidth - xPos <= ellipsisWidth)
{
part = new TextPart();
part.xPos = xPos;
part.yPos = yPos;
part.startIndex = textOffset;
part.text = "...";
part.width = ellipsisWidth;
part.style = style;
part.link = null;
parts.addElement(part);
}
} else
{
xPos = 0;
lineNr++;
yPos += style.font.getHeight();
}
continue; /* Loop/switch isn't completed */
}
part = new TextPart();
part.xPos = xPos;
part.yPos = yPos;
part.startIndex = textOffset;
part.text = src.substring(textOffset, textOffset + nrPartChars);
if(nrPartChars >= charsBeforeTag || ellipsisWidth <= 0) goto _L2; else goto _L1
_L1:
new StringBuffer();
part;
JVM INSTR dup_x1 ;
text;
append();
"...";
append();
toString();
text;
_L2:
part.width = style.font.stringWidth(part.text);
part.style = style;
part.link = link;
parts.addElement(part);
if(nrPartChars <= 0)
done = true;
else
if(nrPartChars < charsBeforeTag)
{
for(textOffset += nrPartChars; textOffset < totalNrChars && src.charAt(textOffset) == ' '; textOffset++);
xPos = 0;
lineNr++;
yPos += style.font.getHeight();
} else
{
textOffset += charsBeforeTag;
xPos += part.width;
}
if(true) goto _L4; else goto _L3
_L3:
return parts;
}
public static final Image createStringImage(String str, Font font, int color)
{
return createStringImage(str, font, color, 0, 0);
}
public static final Image createStringImage(String str, Font font, int color, int hPadding, int vPadding)
{
int fontHeight = font.getHeight();
int stringWidth = font.stringWidth(str);
if(stringWidth == 0)
return null;
Image temp = Image.createImage(stringWidth + hPadding * 2, fontHeight + vPadding * 2);
int width = temp.getWidth();
int height = temp.getHeight();
Graphics graphics = temp.getGraphics();
graphics.setFont(font);
graphics.fillRect(0, 0, width, height);
graphics.setColor(255);
graphics.drawString(str, hPadding, vPadding, 20);
int rgb[] = new int[width * height];
temp.getRGB(rgb, 0, width, 0, 0, width, height);
graphics = null;
temp = null;
int alpha = color >>> 24;
if(alpha == 0 || alpha == 255)
{
for(int i = rgb.length - 1; i >= 0; i--)
rgb[i] = rgb[i] << 24 | color;
} else
{
color &= 0xffffff;
for(int i = rgb.length - 1; i >= 0; i--)
rgb[i] = (rgb[i] * alpha >> 8) << 24 | color;
}
return Image.createRGBImage(rgb, width, height, true);
}
public static void paintFreeMem(Graphics g, Font font, int color, int x, int y, int anchor)
{
g.setColor(color);
g.setFont(font);
System.gc();
g.drawString("Free: " + Runtime.getRuntime().freeMemory() / 1000L + " / " + Runtime.getRuntime().totalMemory() / 1000L, x, y, anchor);
}
private static Random random;
private static int smallHeight;
private static int mediumHeight;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?