android-gif-drawable
android-gif-drawable copied to clipboard
MultiCallback class must have own Handler
If a Drawble, which has a MultiCallback, schedules a task, this task will be executed in every Drawable.Callback member of MultiCallback. It will be correctly to set a Handler to schedule tasks in constructor or setter of MultiCallback. Handler must be single per every Drawable.
Why Handler
should be used there?
Not Handler
exactly, it may be any main-thread executor. But, Android's View
class uses Handler
to schedule Drawable
's tasks. Unfortunately, Android's Handler
doesn't implements Java's abstract interface java.util.concurrent.Executor
.
I meant why any additional mechanism should be used?
If Drawable
wants to schedule some task, it can use it's Callback
. But if this Callback
has MultiCallback
implementation, the task will be executed by each of the members of MultiCallback
.
For example, my Drawable
wants to implement some animation. It calls Drawable#getCallback().scheduleDrawable(Drawable, Runnable, long)
, where Runnable
has some counter (increment). If Drawable#getCallback()
returns MultiCallback
, the task will be executed many times (the counter will be incremented many times) by only single call of scheduleDrawable
. Drawable
doesn't expect such behavior.
OK, I see now, however there are use cases where executing Runnable
on each callback is intended.
Eg. I have a single instance of custom GifDrawable
subclass which I want do display in several custom ImageView
s which do some extra action apart from displaying frames which is fired when Runnable
is scheduled. For example several fade-out steps while displaying given frame. In such case View
could change its alpha inside scheduleDrawable()
. That action must be executed for each View
not only for one.
Another important issue is that callback implementation may decide to not execute Runnable
eg. View
calls first verifyDrawable()
and then optionally enqueues Runnable
.
So to be complete it can be configurable eg. in MultiCallback
constructor if Runnables
are dispatched to each child callback or executed once as you said.
You can implement both of these: single executor and executors from Callback
members.