Android下將文字轉換成圖片得方法,項目已實際應用


跟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;
	}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章