次のようなテキスト<b style="color: rgb(255, 0, 0);">Test</b>
がありHtml.fromHtml
ます。Spanned
StaticLayout
Canvas
Bitmap
描画される画像には、この太字のテキストが含まれています。ただし、後者は 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);
}