AlphaAnimation在冰激凌三明治中无法工作

4

我通过使用 AlphaAnimation 调整 alpha 值来实现淡入淡出动画的布局展示。

目前这种方案在使用 Gingerbread 两款设备以及一个使用 Froyo 的设备上都能正常工作。但是,在 Ice Cream Sandwich 上却不能运行?!

下面是我的 xml 布局。

<LinearLayout
        android:id="@+id/photoOptionBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:alpha="0"
        android:background="@color/gpsBackground"
        android:orientation="horizontal">

        <ImageButton
            android:id="@+id/displayShareBtn"
            style="@android:style/Widget.Holo.Button.Borderless.Small"
            android:background="@android:color/transparent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:padding="20dp"
            android:src="@drawable/share"
            android:contentDescription="Share the picture"/>

        <ImageButton
            android:id="@+id/displayDiscardBtn"
            style="@android:style/Widget.Holo.Button.Borderless.Small"
            android:background="@android:color/transparent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:padding="20dp"
            android:src="@drawable/contentdiscard"
            android:contentDescription="Discard Picture"/>

    </LinearLayout>

以下是我的onCreate方法,用于在加载时将透明度设置为0,因为在xml中进行此操作时它不起作用...

    AlphaAnimation alpha = new AlphaAnimation(0.0F, 0.0F);
    alpha.setDuration(0); // Make animation instant
    alpha.setFillAfter(true); // Tell it to persist after the animation ends
    // And then on your layout
    this._photoOptionsBar.startAnimation(alpha);

然后当我想要展示它时,我调用以下内容。

public void showOptions() {
        AlphaAnimation alpha = new AlphaAnimation(0.0F, 1.0F);
        alpha.setDuration(PhotoDisplayFragment.FADE_DURATION); // Make animation instant
        alpha.setFillAfter(true);
        this._photoOptionsBar.startAnimation(alpha);
        this._optionsShowing = true;
}

当我在IceCream sandwich中加载时,LinearLayout是存在的,因为你可以点击按钮并执行它们的功能,显示函数也被调用了,但是我就是看不到它!任何帮助都将是非常棒的。
2个回答

6
问题出在线性布局中的android:alpha=0。为了让它正常工作,您需要在布局xml中将可见性属性设置为不可见。并且在开始 alpha 动画之前调用 setVisibility(View.VISIBLE)

你对问题所在的位置是正确的,但将其设置为xml文件中的1使其余部分正常工作。 - StuStirling
因为计时器的原因,我没办法。 - StuStirling

2

我曾经遇到过完全相同的问题。

似乎有两个不同的alpha属性。AlphaAnimation更改其中一个,而android:alpha更改另一个。这两个属性都在ICS上使用,但是在ICS之前,android:alpha将被忽略。

因此,如果您设置了android:alpha="0"并使用AlphaAnimation进行动画处理,则在ICS之前,它将正常工作,因为android:alpha将被忽略。在ICS上,视图始终不可见,因为AlphaAnimation不会将android:alpha从0更改。因此,解决方案就是简单地删除android:alpha="0"行。

我不确定这一点是否完全正确,但它看起来是这样的。

我还认为这两个alpha的工作方式略有不同。如果您更改具有半透明图像的ImageView的alpha值,则使用AlphaAnimationandroid:alpha效果要差得多。但是您无法做太多关于这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接