使用Paint绘制HTML

时间:2016-01-27 21:55:27

标签: android bitmap google-maps-android-api-2

我试图通过生成位图并将其作为标记放置在Google地图片段上来绘制文字。但是,文本需要通过HTML进行风格化。我想知道这是否可能。

max-height:100%;

示例HTML

我主要需要它来渲染public static Bitmap createPureTextIcon(String text){ Paint textPaint = new Paint(); //textPaint.setTypeface(TypefaceUtil.get(m)) textPaint.setTextSize(MAP_DISPLAY_TEXT_SIZE); textPaint.setAntiAlias(true); textPaint.setTextAlign(Paint.Align.LEFT); float baseline = -textPaint.ascent(); // ascent() is negative int width = (int) (textPaint.measureText(text) + 0.5f); // round int height = (int) (baseline + textPaint.descent() + 0.5f); CharSequence m = ViewUtil.noTrailingwhiteLines(Html.fromHtml(text)); //height * 2 Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(image); canvas.drawText(m.toString(), 0, baseline, textPaint); //canvas.translate(0, height*4); return image; }

<br />

2 个答案:

答案 0 :(得分:0)

使用StaticLayout

    StaticLayout layout = new StaticLayout(Html.fromHtml(m.toString()), textPaint, width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
    layout.draw(canvas);

答案 1 :(得分:-1)

尝试canvas.drawText(HTML.fromHtml(text), 0, baseline, textPaint); fromHTML将HTML字符串转换为可绘制的Spannable。