I'm currently working on a random-lightning-generator class in my app. Lightning (extends View) is a random blue path that reveals in a phase and when it's revealed it fades out.
I want the lightning to regenerate and show on the canvas again (for now just once, I will later control its repeat-frequency).
The lightning-objects class builds its path and draw it on the canvas once. Right now I succeed creating a new lightning using the invalidate()
method (to call the onDraw()
method), but the lightning won't show on the canvas.
What can I do in order for the regenerated ones to show up? Thank you in advance (:
OK, here is what is going on:
EDIT
As a reminder: The root problem here is that the View's
alpha
is getting stuck at 0, which means that all drawing operations are hidden, once the first lightning fades out.
In my first revision, I wasn't as specific as I should have been. I just wanted you to change this line:
final ObjectAnimator alpha = ObjectAnimator.ofFloat(RandomLightning.this, "alpha", 0);
to this:
final ObjectAnimator alpha = ObjectAnimator.ofFloat(RandomLightning.this, "alpha", 1.0f, 0);
That, combined with the init2()
fix (more on that later), is enough to get things re-drawing at regular intervals. But later, I realized it had a bug: after the 1st lightning, the "phase" animation would stop showing up.
So, don't fix it that way. Instead, just add a call to the top of animateLightning()
:
setAlpha( 1.0f );
This causes the alpha
to reset to 1 at the start of each new lightning, thus putting the state machine back in the original starting state.
Do not add a new ObjectAnimator
to the Runnable
; it doesn't do anything useful.
ABOUT init2():
I'm not sure why you think the init2()
in the Runnable
is unnecessary or redundant. You seem to have a misunderstanding about the control flow, but I can't put my finger on what it is.
init2()
regenerates your lightning, and restarts the animation state machine. If it is not called in the Runnable
, nothing else will call it (you should be able to confirm this using the log), and that means no new lightning, and no animation. (If you disagree, please feel free to comment.)
It should be clear to you that the first lightning only happens because you call init1()/init2()
in all the constructors. The second, and all later lightnings, only happen because that Runnable
starts executing 5 seconds after the View
is created.