【问题标题】:AlphaAnimation object don't change alpha property of the view being animated, why?AlphaAnimation 对象不会改变被动画视图的 alpha 属性,为什么?
【发布时间】:2015-02-07 21:20:36
【问题描述】:

我正在尝试为按钮的 alpha 设置动画,当我将 alpha 从 1 设置为 0 时效果很好。但是,在动画的最后,我无法将其从 0 重置为 1,因为按钮的alpha 已经是 1(这会导致按钮在屏幕上“跳跃”而没有任何淡入淡出)。似乎 Animation 对象不是直接设置视图 alpha,而是视图的一些表示属性。有谁知道我怎样才能让这个动画正常工作?

我的代码:

private void performFavoriteButtonFade(boolean isFavorite) {
    AlphaAnimation fadeAnimation = new AlphaAnimation(0, 0);
    if (isFavorite) {
        if (this.favoriteButton.getAlpha() == 1) {
            fadeAnimation = new AlphaAnimation(1, 0);
        }
    } else {
        if (this.favoriteButton.getAlpha() == 0) {
            fadeAnimation = new AlphaAnimation(0, 1);
        } else {
            fadeAnimation = new AlphaAnimation(1, 1);
        }
    }

    fadeAnimation.setDuration(300);
    fadeAnimation.setFillAfter(true);

    this.favoriteButton.startAnimation(fadeAnimation);
    this.favoriteButton.setVisibility(View.VISIBLE);
}
<ImageButton
        android:id="@+id/favoriteButton"
        android:src="@drawable/favorite_icon"
        android:background="@android:color/transparent"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="30dp"
        android:layout_marginBottom="20dp"
        android:visibility="invisible"
        android:onClick="didTapNotFavorite"
        />

注意事项:

由于answer in this post,我正在设置视图可见性以使 AlphaAnimation 正常工作。

【问题讨论】:

    标签: android android-animation


    【解决方案1】:

    您似乎在使用旧式动画。您可以使用新样式,它应该可以在不改变可见性或填充之后工作。无需在 XML 中设置可见性,只需将 alpha 设置为 0。

    淡出:

    Button button = new Button(mActivity);
    button.animate().alpha(0).setDuration(300).start();
    

    淡入:

    Button button = new Button(mActivity);
    button.animate().alpha(1f).setDuration(300).start();
    

    【讨论】:

      【解决方案2】:

      试试这个:

      AlphaAnimation fadeAnimation;
              if (isFavorite) {
                  if (this.favoriteButton.getAlpha() == 1) {
                      fadeAnimation = new AlphaAnimation(1, 0);
                      fadeAnimation.setInterpolator(new AccelerateInterpolator());
                  }
              } else {
                  if (this.favoriteButton.getAlpha() == 0) {
                      fadeAnimation = new AlphaAnimation(0, 1);
                      fadeAnimation.setInterpolator(new DecelerateInterpolator());
                  } else {
                      fadeAnimation = new AlphaAnimation(1, 1);
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多