Forum > Android

Drawing text on Tbitmap canvas


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
it crashes.
Is TBitmap.canvas.TextOut not supported?

Many thanks.

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.
2> Draw the text using LazFreeType which is non-native, see here:
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.

Thanks Filipe.
I've used
but the procedure SyncScreenWrite doesn't get called.
I added cthreads to the uses, is there anything else synchonize needs?

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

I've tried LazFreeType. It works well but is a bit slow to draw.
Thanks for all your help!


[0] Message Index

Go to full version