0

I want to get the bitmap that is drawn when a textview is displayed but without displaying the textview in the activity. something like this:

TextView t = new TextView(this);
t.forceToDrawItself();
Bitmap b=t.getViewBitmap();

how is this possible?

Simon
  • 13,173
  • 14
  • 66
  • 90

1 Answers1

2

View#draw(Canvas) will draw the entire view into the given Canvas. You can use the constructor Canvas(Bitmap) to create a Canvas that draws into the given Bitmap.

Create a bitmap of the desired size with Bitmap#createBitmap(int, int, Bitmap.Config), wrap it in a canvas, and pass it to your view's draw method.

adamp
  • 28,862
  • 9
  • 81
  • 69
  • ok works fine. now i have the problem that i have to set the size of the view manually.. is there a method to calc the default size of a view? like t.initSize(); ? – Simon Jun 14 '10 at 11:13
  • Take a look at the "Layout" section of the View class documentation. (http://developer.android.com/reference/android/view/View.html) You will want to call View#measure with MeasureSpecs for width and height. – adamp Jun 14 '10 at 18:39
  • It helped me. But I'm facing one problem. I need to center align the text. I tried setGravity(Gravity.CENTER) but it's not giving the required output. Any suggestions? – Ammar May 13 '14 at 09:04