I have found a solution for you. First I was going to get the text from the TextView, calculate the lines and then draw it on the bitmap. It might work, but I tried something else and it worked well.
To enable the scrollable TextView application, as a rule, do what you did, set scrollbars:"vertical"
and set ScrollingMovementMethod()
.
Alternatively, you can simply wrap your text view inside a ScrollView, for example.
<ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/scrollView" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" > <TextView android:id="@+id/textView" android:text="@string/hello_world" android:gravity="center_horizontal" android:textSize="20sp" android:typeface="monospace" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </ScrollView>
then this code will work
tv.setDrawingCacheEnabled(true); Bitmap testB = Bitmap.createBitmap(tv.getDrawingCache());
Delete lines:
tv.setMovementMethod(new ScrollingMovementMethod()); tv.buildDrawingCache();
Now let me explain why this works. When you request caching for a drawing, you get a cache based on the boundaries of the views and everything that is drawn on those borders. Since your TextView matches RelativeLayout and draws invisible text from borders, you cannot get all the text in your bitmap.
On the other hand, when you wrap it in a ScrollView, the size of the TextView matches the text, so the caching borders cover the entire text and are great for you.
source share