[英]Create an image from a TextView (without losing quality)
我有一個應用程序,將創建用戶在文本字段中寫入的任何內容的png圖像(自定義字體和顏色)
我正在做的是這樣的:
使用下面的代碼,為TextView生成圖像
Bitmap returnedBitmap = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(), Bitmap.Config.ARGB_8888); //Bind a canvas to it Canvas canvas = new Canvas(returnedBitmap); //Get the view's background Drawable bgDrawable =textView.getBackground(); if (bgDrawable!=null) //has background drawable, then draw it on the canvas bgDrawable.draw(canvas); // draw the view on the canvas view.draw(canvas); FileOutputStream out = null; out = new FileOutputStream(new File(filename)); returnedBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
問題是,生成的圖像的質量與TextView中顯示的質量不同
有沒有辦法提高質量?
是因為TextView沒有背景嗎? 如果是這樣,是否有解決方法? 需要生成的圖像只是文本,因此透明背景
謝謝
我相信質量取決於您創建圖像時的屏幕尺寸。 我建議你把Textview放在FrameLayout中並從FrameLayout創建位圖。
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.