跟ios一樣,還是解決分享長微博問題,在網上搜過一些方法,測試都有問題,後來根據起方法進行改進,形成該方法;現在在項目中應用,目前沒有發現大得問題。
現分享一下。
public static Bitmap textAsBitmap(String text, float textSize) {
TextPaint textPaint = new TextPaint();
// textPaint.setARGB(0x31, 0x31, 0x31, 0);
textPaint.setColor(Color.BLACK);
textPaint.setTextSize(textSize);
StaticLayout layout = new StaticLayout(text, textPaint, 450,
Alignment.ALIGN_NORMAL, 1.3f, 0.0f, true);
Bitmap bitmap = Bitmap.createBitmap(layout.getWidth() + 20,
layout.getHeight() + 20, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.translate(10, 10);
canvas.drawColor(Color.WHITE);
layout.draw(canvas);
Log.d("textAsBitmap",
String.format("1:%d %d", layout.getWidth(), layout.getHeight()));
return bitmap;
}