How to avoid OutOfMemory for a TextView containing unlimited ImageSpans?

I am developing an application for writing handwriting.
The application works this way: the user writes (draws) text on the touch screen, the application converts the handwriting to bitmap (getBitmapFromHandwriting ()), and then uses this bitmap to create a SpannableString and shows it to the user in a TextView (In my case, I use EditText, but to make things simple, say TextView).
Here is a snippet of code in an extended TextView:

class MyTextView extends TextView{ ... BitmapDrawable bmd = new BitmapDrawable(getBitmapFromHandwriting()); int width = bmd.getIntrinsicWidth(); int height = bmd.getIntrinsicHeight(); bmd.setBounds( (int)(height/4.0f), (int)(height/4.0f), (int)(width + height/4.0f), (int)(height*5.0f/4.0f)); final ImageSpan img = new ImageSpan(bmd); getText().setSpan(img, position, position+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); … } 

In getBitmapFromHandwriting (), the application creates a new bitmap for each text drawn by the user.

Everything works fine before the note gets large (which means there are a lot of bitmaps in the native memory), OutOfMemoryError is thrown when creating Bitmap ().
After many searches, I learned that bitmaps use "native memory", which is very limited.
I also found out that gc for a bitmap does not happen at the same time as gc JVM. Therefore, people are better off doing bitmap.recycle () when the bitmap is definitely no longer needed.
From the above knowledge, I think that I need to process bitmaps for invisible texts and load this bitmap only when necessary (i.e., Apparently)
Before processing the bitmap, I have to make sure that it will not be used in the future, otherwise I get "RuntimeException: Canvas: attempt to use the processed bitmap ..."
that is, I have to clear the corresponding spaces attached to the SpannableString. (GetText (). RemoveSpan () or getText (). ClearSpans (), but that's another story.)
To do this, I need to indicate which lines are visible and which are invisible. In ViewGroups such as GridView, I can call "getFirstVisiblePosition ()", but I don't know how to do this for TextView.

I know that you can reset a bitmap sample to support more bitmaps, but I want TextView to support UNLIMITED ImageSpans.
If it is a GridView or ListView, lazy loading will be performed. But how to do it in one TextView?
Splitting content into multiple pages should theoretically work in some situations, but in my application the user can also “export” the note. Exporting simply makes a copy of the current view (how to export without creating a new bitmap, this is another problem not discussed in this thread). If the note is divided into several pages, the user must "export" for each page, so this is not an option.

My questions:
1. Is it possible to define visible lines in a TextView?
2. Is there a better way (templates) to implement such a “TextView containing multiple ImageSpans” without receiving an OutOfMemoryError?

Any idea is appreciated. Thanks in advance!

+4
source share

All Articles