我尝试将Unicode字符串绘制到SurfaceView,但我无法使其工作。这是我的代码。
public class TestView extends SurfaceView implements SurfaceHolder.Callback {
private Paint painter = null;
public LyricView(Context context) {
super(context);
initialize();
}
protected void initialize() {
getHolder().addCallback(this);
setFocusable(true);
painter = new Paint(Paint.ANTI_ALIAS_FLAG);
painter.setStyle(Paint.Style.STROKE);
painter.setStrokeWidth(3);
painter.setColor(Color.WHITE);
painter.setTextSize(50);
}
...
...
@Override
protected void onDraw(Canvas canvas) {
String test = "日本語";
canvas.drawText(test, 100, 100, painter);
}
}
如果我将我的字符串更改为unicode escape,如下所示,它可以工作。我不知道为什么。
String test = "\u65E5\u672C\u8A9E";
请帮忙。
谢谢。
答案 0 :(得分:0)
使用此解析器将字符串转换为Unicode
public class UnicodeString {
public static String convert(String str) {
StringBuffer ostr = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if ((ch >= 0x0020) && (ch <= 0x007e)) // Does the char need to be converted to unicode?
{
ostr.append(ch); // No.
} else // Yes.
{
ostr.append("\\u"); // standard unicode format.
String hex = Integer.toHexString(str.charAt(i) & 0xFFFF); // Get hex value of the char.
for (int j = 0; j < 4 - hex.length(); j++)
// Prepend zeros because unicode requires 4 digits
ostr.append("0");
ostr.append(hex.toLowerCase()); // standard unicode format. ostr.append(hex.toLowerCase(Locale.ENGLISH));
}
}
return (new String(ostr)); // Return the stringbuffer cast as a string.
}
}