简体   繁体   中英

Create an image from a TextView (without losing quality)

I have an app that will create a png image of anything the user write in the text field (custom font and color)

What I'm doing is something like this:

  1. Set Typeface, Text Color and Text to a TextView (BG is transparent)
  2. Using the code below, generate the image for the 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); 

The problem is that, the quality of the generated image is not the same as what is displayed in the TextView

Is there any way to improve the quality?

Is it because of the TextView not having a background? If so, is there a workaround for this? Need the generated image to only be the text, hence the transparent background

Thanks

I believe the quality depends on the screen size from you create the image. I suggest you to put the Textview in a FrameLayout and create the bitmap from the 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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM