Convert full scrollable text to bitmap

I am trying to convert a text image to a bitmap here is my Xml code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@string/Rlayout" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".ProductListAcivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_above="@+id/textView2" android:layout_toLeftOf="@+id/bntLinLay" android:layout_toRightOf="@+id/scrollView1" android:gravity="center_horizontal" android:scrollbars="vertical" android:text="****MIMOSA**** \n Articles :\n" android:typeface="monospace" /> </RelativeLayout> 

then:

 TextView tv = (TextView) findViewById(R.id.textView1); tv.setMovementMethod(new ScrollingMovementMethod()); tv.setDrawingCacheEnabled(true); tv.buildDrawingCache(); Bitmap testB = Bitmap.createBitmap(tv.getDrawingCache()); 

but the bitmap size is the initial size of the TextView and only takes up the displayed part of my Textview, so I tried this code with scrallable linearLayout and it works! I can not find a solution. Any suggestion please!

+1
source share
1 answer

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.

0
source

Source: https://habr.com/ru/post/1413443/


All Articles