2016-01-27 55 views
2

我試圖通過生成位圖並將其放置爲標記來在Google Map碎片上繪製文本。但是,文本需要通過HTML進行風格化。我想知道這是否可能。使用Paint繪製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; 
} 

例HTML

我主要是需要它來渲染<br />

<p style=\"text-align: center;\">Looking<br />at Earth </p> 

回答

-1

嘗試canvas.drawText(HTML.fromHtml(text), 0, baseline, textPaint); fromHTML HTML字符串轉換成可以得出一個Spannable。

+0

我,看到'的CharSequence M = ViewUtil.noTrailingwhiteLines(Html.fromHtml(文本));' 它不是在所有 –

+0

渲染HTML這是行不通的。 – Oliv

0

使用StaticLayout

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