Forum > Android
Drawing text on Tbitmap canvas
(1/1)
BigSid56:
I need to draw text on to a bitmap in one thread and then copy the bitmap to a subcontrol in the main thread.
When I try to use
ScreenBitmap.canvas.TextOut(X,Y,atext);
it crashes.
Is TBitmap.canvas.TextOut not supported?
Many thanks.
felipemdc:
It is supported, of course, but only from the main thread. TextOut uses the native Android text drawing APIs, which are accessed via JNI. Any kind of JNI call from a non-registered thread will necessarely crash. The JNI pointers are invalid in other thread. You could call AttachCurrentThread, but the LCL code is not prepared to use another JNI pointer which it would receive.
So you have various options:
1> Call text drawing routines in a Synchronize call, which will make it run in the main thread. The rest of the drawing routines are non-native, so they should work fine in the secondary thread.
or
2> Draw the text using LazFreeType which is non-native, see here: http://wiki.lazarus.freepascal.org/LazFreeType
or
3> Send a patch or open a bug report for adding support via a define in CustomDrawn-Android for using LazFreeType instead of the native text drawing APIs. LazFreeType has more changes of working in secondary threads although I never tested it. It shouldn't be a hard task, but is not a huge priority for me.
BigSid56:
Thanks Filipe.
I've used
synchronize(SyncScreenWrite);
but the procedure SyncScreenWrite doesn't get called.
I added cthreads to the uses, is there anything else synchonize needs?
felipemdc:
Ummmm, usually syncronize is called in the main loop. But in Android we have no access to the main loop. So I guess that the main thread would need to have a TTimer running which calls CheckSynchronize(). To me it looks like it would be better in your case to use LazFreeType
BigSid56:
I've tried LazFreeType. It works well but is a bit slow to draw.
Thanks for all your help!
Navigation
[0] Message Index