Android - Off-screen drawing from non-UI thread

Short verson

Is this allowed or do I need to use a UI thread?

EDIT: A link to a place in Android white papers would be perfect.

Long version

Android docs clearly state that they are not allowed to "access the Android UI toolkit from outside the user interface."

On the other hand, the creation of Bitmap objects from workflows is apparently allowed, at least this is done in the code example: http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html Apparently, the Bitmap class is not considered part of the "user interface toolkit" in relation to streaming.

I have a piece of code that seems to work when called from a thread other than ui. It includes the use of Bitmap.createBitmap (int, int, Bitmap.Config), the new Canvas (bitmap), Typeface.create (), and a text drawing. My code does not reference any View object.

Can someone point me to the part of the documentation that says I can do this from a background thread? Or will it lead to random crashes?

+4
source share
3 answers

The UI toolkit means user interfaces such as buttons, tags, lists, etc. provided by Google. You cannot access them from a thread other than ui, mainly because they are not thread safe.

What you are doing is not a set of user interface tools, but on a low-level canvas that is allowed (should actually be allowed) to access from threads other than ui. This mechanism is used in game development all the time. Therefore, I believe that you are safe.

+2
source

Updates to the views must be done in the user interface thread or from a remote thread with the post function (which basically indicates the UI thread that the remote thread wants to do something), which is part of the View class.

0
source

This should be legal, because bitmaps just sit in memory on their own before drawing them.

I am studying the development of the game, it seems that there are some threads that update the user interface (using a similar technique for rendering in memory)

0
source

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


All Articles