0

I try to draw Unicode string to SurfaceView, but I cannot get it work. Here is my code.

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);
    }
}

if I change my string to unicode escape as below, it work. I don't know why.

String test = "\u65E5\u672C\u8A9E";

Please help.

Thank you.

4

1 回答 1

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.
    }
}
于 2013-08-10T10:50:57.270 回答