【发布时间】:2014-11-22 04:57:28
【问题描述】:
我正在构建一个看起来像这样的组件:
<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>
这个想法是用户将自定义 FrameLayout 放置在某处并填充内容,当它被构建时,它将以编程方式添加一个具有零 alpha 的 View 覆盖整个布局,然后是一个短而宽的栏顶部用于警报/通知。当触摸警报栏以展开时,覆盖层的 alpha 将动画到 50% 左右,以使无法触摸的区域变灰。就像DialogFragment 显示时屏幕的其余部分如何变灰一样。
最初的叠加很简单,例如:(为简洁起见,省略了一些关键字)
<View alpha="0.0"/>
然后就膨胀了。
在FrameLayout的类中,我创建了几个动画:
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);
当触摸动作发生时,我只是将它们称为overlay.startAnimation(fadeInAnimation)。
奇怪的行为是,如果叠加层被初始化为一些小的 alpha,动画不会做任何事情。我可以验证它至少被调用了,但 alpha 保持不变。
但是,如果我将叠加层的 alpha 保留为其默认值 1.0,并在构造时将其可见性设置为 INVISIBLE,则动画将正确触发。如果起始 alpha 为 0.6 之类的,这也适用。 fadeInAnimation 捕捉到 0 alpha 并将其提高到 0.5,然后其余部分正常运行。
我还没有测试过 alpha 中断的位置是否有一些下限,或者动画的范围与哪些值无效之间是否存在某种关系。
有什么想法吗?
【问题讨论】:
-
@StoneBird 看起来我没有正确搜索。这似乎不直观。如果您想将其发布为答案,我会给您赏金。
-
见贴。并添加了一条建议。