【问题标题】:AlphaAnimation does not workAlphaAnimation 不起作用
【发布时间】:2012-07-08 08:57:48
【问题描述】:

我一直在寻找解决问题的方法。但我的代码似乎没问题。

我将尝试解释:我的布局定义中有一个带有 android:alpha="0" 的 TextView。我想(当单击图像时)显示带有 AlphaAnimation 的 TextView,从 0.0f 到 1.0f。

我的问题是,当我单击图像时,什么也没有发生。但奇怪的是,如果我在布局定义中将它的 alpha 设置为 1,然后单击图像,我可以看到动画(alpha 1 -> alpha 0 -> alpha 1)。

我做错了什么?

我的代码:

TextView tv = (TextView) findViewById(R.id.number);

AlphaAnimation animation1 = new AlphaAnimation(0.0f, 1.0f);
animation1.setDuration(1000);
animation1.setFillAfter(true);
tv.startAnimation(animation1);

提前致谢。

【问题讨论】:

    标签: android animation alpha


    【解决方案1】:

    问题出在android:alpha="0"。该属性设置 View http://developer.android.com/reference/android/view/View.html#attr_android:alpha

    的透明度

    当 alpha 属性等于 0 时,动画将透明度从 0*0.0f=0 更改为 0*1.0f=0。当 alpha 属性设置为 1 时,动画会将透明度从 1*0.0f=0 更改为 1*1.0f=1。这就是为什么在第一种情况下您看不到文本,而在第二种情况下一切正常。

    要使事情正常工作,您必须在布局 xml 中将可见性属性设置为不可见。在开始 alpha 动画之前调用tv.setVisibility(View.VISIBLE);

    【讨论】:

    • 感谢 vasart 的回复。我到家时会试试的。但是,我认为这样做,在动画开始之前我会看到 TextView 元素将出现 alpha 1,然后它将开始其动画。那正确吗?我想要做的是 TextView 在我的视图中缓慢出现。
    • 它应该会慢慢出现,启动时不会闪烁
    • 我似乎是部分响应,如果我设置 android:visibility="INVISIBLE or GONE" 会发生什么,alpha 1 的动画不起作用。看起来属性动画师需要视图。
    • 使用Invisible 而不是Gone 是诀窍。消失 == Alpha 0
    【解决方案2】:

    更简单的方法呈现在this答案:

    tv.animate().alpha(1).setDuration(1000);
    

    【讨论】:

      【解决方案3】:

      实际上,android 有两个用于视图的 alpha 属性

          /**
           * The opacity of the View. This is a value from 0 to 1, where 0 means
           * completely transparent and 1 means completely opaque.
           */
          @ViewDebug.ExportedProperty
          float mAlpha = 1f;
      
          /**
           * The opacity of the view as manipulated by the Fade transition. This is a hidden
           * property only used by transitions, which is composited with the other alpha
           * values to calculate the final visual alpha value.
           */
          float mTransitionAlpha = 1f;
      
      
      /**
       * Calculates the visual alpha of this view, which is a combination of the actual
       * alpha value and the transitionAlpha value (if set).
       */
      private float getFinalAlpha() {
          if (mTransformationInfo != null) {
              return mTransformationInfo.mAlpha * mTransformationInfo.mTransitionAlpha;
          }
          return 1;
      }
      

      最终的 alpha 是两个 alpha 的乘积

      View#setAlpha(float) & View#animate() & android:alpha -> mAlpha

      AlphaAnimation -> mTransitionAlpha

      【讨论】:

        【解决方案4】:

        将动画的 fillBefore 属性设置为 true 为我解决了这个问题。

        TextView tv = (TextView) findViewById(R.id.number);
        
        AlphaAnimation animation1 = new AlphaAnimation(0.0f, 1.0f);
        animation1.setDuration(1000);
        animation1.setFillBefore(true);
        tv.startAnimation(animation1);
        

        FillBefore 在开始动画之前设置变换。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-02-13
          • 2013-09-27
          • 1970-01-01
          • 1970-01-01
          • 2023-03-22
          • 2016-03-13
          • 1970-01-01
          相关资源
          最近更新 更多