1

我有一个像这样的文本:<b style="color: rgb(255, 0, 0);">Test</b>它被赋予Html.fromHtml调用,它返回一个Spanned用于在绑定到 aStaticLayout的 Android 上绘制的a 。CanvasBitmap

绘制的图像包含此粗体文本。但后者不是红色的,这虽然是由 CSS 内联样式定义的。

我知道该选项Html.FROM_HTML_OPTION_USE_CSS_COLORS存在,但是(正如我可以在文档中隐含地阅读的那样)它没有这个目的。

来源

注意:在下面的代码中,mTextPaint.setColor(Color.WHITE);被注释掉了。文本在黑色背景上涂成黑色。但不是红色的。所以问题不在于mTextPaint.setColor会覆盖我的 CSS 内联样式颜色。

private BitmapDrawable addTextOnImage(BitmapDrawable bitmapDrawable) {

    Spanned caption = Html.fromHtml("<b style=\"color: rgb(255, 0, 0);\">Test</b>", Html.FROM_HTML_MODE_LEGACY);

    Bitmap bitmap_largest_image = bitmapDrawable.getBitmap().copy(Bitmap.Config.ARGB_8888, true);

    TextPaint mTextPaint = new TextPaint();
    //mTextPaint.setColor(Color.WHITE);
    mTextPaint.setTextSize(30);
    StaticLayout mTextLayout_caption = StaticLayout.Builder.obtain(caption, 0, caption.length(), mTextPaint, bitmap_largest_image.getWidth()).build();

    Paint backgrounds_paint = new Paint();
    backgrounds_paint.setColor(Color.BLACK);
    backgrounds_paint.setStyle(Paint.Style.FILL);

    Bitmap final_bitmap = Bitmap.createBitmap(bitmap_largest_image.getWidth(), mTextLayout_caption.getHeight() + bitmap_largest_image.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(final_bitmap);
    canvas.drawRect(0, 0, bitmap_largest_image.getWidth(), mTextLayout_caption.getHeight(), backgrounds_paint);
    mTextLayout_caption.draw(canvas);
    canvas.drawBitmap(Bitmap.createScaledBitmap(bitmap_largest_image, bitmap_largest_image.getWidth(), bitmap_largest_image.getHeight(), false),  0, mTextLayout_caption.getHeight(), new Paint());

    return new BitmapDrawable(getActivity().getResources(), final_bitmap);
}
4

0 回答 0