1

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

Michael Spitsin
  • 2,539
  • 2
  • 19
  • 29
kishidp
  • 1,918
  • 6
  • 23
  • 29
  • you can take a screenshot http://stackoverflow.com/a/5651242/3257513 – Yoni Jul 27 '16 at 07:50
  • Try `textview.setDrawingCacheEnabled(true); Bitmap bitmap = Bitmap.createBitmap(textview.getDrawingCache()); textview.setDrawingCacheEnabled(false);` – Akshay Bhat 'AB' Jul 27 '16 at 07:51
  • Your size of bitmap depends on width and height of textView which depends (besides from length of text and textSize) on screen metrics. For example, for mdpi you will have 100 pixel height bitmap and for xhdpi 200 pixel height. Does you accept this concept, or you want to return bitmap with strict height (that depends only from textSize)? – Michael Spitsin Jul 27 '16 at 07:54

1 Answers1

0

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;

}

LvN
  • 701
  • 1
  • 6
  • 22