I'm building a component that looks something like this:
<FrameLayout>
// The following two are added programmatically during construction
<View> // match-parent in both dimensions, act as an overlay
<RelativeLayout> // Bar across the top of the component, e.g. notification bar type thing
// User's content goes here
</FrameLayout>
The idea is that user will place the custom FrameLayout
somewhere and populate it with content, and when it's constructed, it will programmatically add a View
with zero alpha that covers the entire layout and then a short and wide bar across the top used for alerts/notifications. When the alert bar is touched to expand, the overlay's alpha is animated to around 50% to gray out the area that cannot be touched. Like how a DialogFragment
will gray out the rest of the screen when shown.
Originally the overlay is simple like: (omitting some keywords for brevity)
<View alpha="0.0"/>
And that gets inflated.
In the FrameLayout
's class, I create a couple animations:
AlphaAnimation fadeInAnimation = new AlphaAnimation(0.0f, 0.5f);
AlphaAnimation fadeOutAnimation = new AlphaAnimation(0.5f, 0.0f);
...
// in some initialization code I put in a couple of extra details
fadeInAnimation.setFillAfter(true);
fadeInAnimation.setDuration(500);
fadeOutAnimation.setFillAfter(true);
fadeOutAnimation.setDuration(500);
And when the touch action occurs, I simply call them like overlay.startAnimation(fadeInAnimation)
.
The strange behavior is that if the overlay is initialized to some small alpha, the animation doesn't do anything. I can verify that it at least gets called, but the alpha stays the same.
However, if I leave the overlay's alpha at its default 1.0, and set its visibility to INVISIBLE
at constructio, the animations will correctly trigger. This also works if the starting alpha is something like 0.6. The fadeInAnimation snaps to 0 alpha and brings it up to 0.5, and then the rest acts normally.
I haven't yet tested to see if there's some lower bound on where the alpha breaks, or if there is some kind of relationship between the range on the animation and which values are invalid.
Any ideas?