1

我有一個應用程序,將創建的任何一個PNG圖像中的文本字段(自定義字體和顏色)用戶寫創建從一個TextView的圖像(而不失品位)

我在做什麼東西像這樣:

  1. 集字體,文字顏色和文本到一個TextView(BG是透明的)
  2. 用下面的代碼,生成圖像的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沒有背景嗎?如果是這樣,是否有解決方法?需要生成的圖像僅是文本,因此透明背景

感謝

+0

你可以採取截圖 http://stackoverflow.com/a/5651242/3257513 – Yoni

+0

試試'textview.setDrawingCacheEnabled(true); 位圖位圖= Bitmap.createBitmap(textview.getDrawingCache()); textview.setDrawingCacheEnabled(false);' –

+0

您的位圖大小取決於textView的寬度和高度,它取決於屏幕度量標準(除了文本和textSize的長度)。例如,對於mdpi,您將擁有100像素高度位圖和xhdpi 200像素高度。你接受這個概念嗎?或者你想以嚴格的高度返回位圖(只依賴於textSize)? –

回答

0

我相信質量取決於屏幕尺寸從您所創建的圖像。我建議你把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; 

}