【发布时间】:2015-07-24 22:27:07
【问题描述】:
所以我试图制作一个像 Trello 一样的 ActionButton(单击一个按钮,一个黑色覆盖层出现,其他按钮出现在屏幕上)。
但我有一个奇怪的问题:我无法使 alpha 动画工作。
我尝试了两种方式:
private void initializeComponent(final Context ctx, final AttributeSet attrs) {
blackOverlay.setAlpha(0.0f);
blackOverlay.setVisibility(View.VISIBLE);
blackOverlay.setClickable(true);
actionButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final boolean closed = blackOverlay.getAlpha() == 0.0f;
blackOverlay.animate().alpha(closed ? 1f : 0f).setDuration(500).setListener(null);
});
}
因此,通过这种方式,视图blackOverlay始终可见,并且动画应该可以工作。但事实并非如此。单击按钮对 alpha 没有任何影响。唯一的变化是切换按钮使非覆盖内容工作/停止工作(因为 blackOverlay 是可点击的)。
所以我尝试了另一种方法:不将 alpha 设置为 0.0f,这样叠加层就会开始对用户可见。基本注释了initializeComponent方法的第一行。
private void initializeComponent(final Context ctx, final AttributeSet attrs) {
//blackOverlay.setAlpha(0.0f);
blackOverlay.setVisibility(View.VISIBLE);
blackOverlay.setClickable(true);
actionButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final boolean closed = blackOverlay.getAlpha() == 0.0f;
blackOverlay.animate().alpha(closed ? 1f : 0f).setDuration(500).setListener(null);
}
});
}
这样,按钮仅切换 alpha,不会对其进行动画处理。
有什么想法吗?我已经在 StackOverflow 上尝试了很多解决方案。
谢谢!
这是我的 xml 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/blackoverlay"
android:background="@color/black_overlay">
</LinearLayout>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/action_button"
android:layout_gravity="bottom"
android:src="@drawable/plus"
android:layout_margin="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
【问题讨论】:
-
这段代码似乎可以正常工作。请创建一个MCVE 并进行验证。
标签: android animation android-linearlayout