【问题标题】:Android Animation AlphaAndroid 动画 Alpha
【发布时间】:2014-01-04 21:49:30
【问题描述】:

我有动画:

<set xmlns:android="http://schemas.android.com/apk/res/android"  
   android:interpolator="@android:anim/linear_interpolator">  
   <alpha  
       android:fromAlpha="0.2"  
       android:toAlpha="1.0"  
       android:duration="500"/>  
</set>

ImageView:

<ImageView
    android:id="@+id/listViewIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/settings" 
    android:alpha="0.2"/>  

和代码:

final Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
final ImageView iv = (ImageView) findViewById(R.id.listViewIcon);
anim .setFillAfter(true);
iv.startAnimation(anim);

所以一开始我有ImageView 和阿尔法0.2,最后我想有ImageView 和阿尔法1。但它不是这样工作的 - 当动画开始时添加更多 alpha 并且动画以 alpha 结束 0.2

我必须更改哪些内容才能将图像从 0.2 变为 1

我检查了不同的设置 - 我设置了 android:alpha="1.0"fromAlpa="1.0"toAlpha="0.2",它的工作方式与我预期的一样 - 从 alpha 10.2。看起来ImageView 的 alpha 乘以动画的 alpha...

【问题讨论】:

  • 要防止视图最后跳回 0.2 alpha,请使用fillAfter 属性:developer.android.com/reference/android/view/animation/…
  • 事实并非如此。 Alpha 不会变为 1。当我从 1->0.2 进行动画处理时,它工作正常并保持在 0.2(之后我使用填充)。当我想从 0.2 动画到 1 时,它会消失到几乎 0 并变为 0.2
  • 您是否将fillEnabled 设置为true?

标签: java android animation kotlin alpha


【解决方案1】:

试试这个

AlphaAnimation animation1 = new AlphaAnimation(0.2f, 1.0f);
animation1.setDuration(1000);
animation1.setStartOffset(5000);
animation1.setFillAfter(true);
iv.startAnimation(animation1);

【讨论】:

  • 从您的设备中删除 imageview 属性中的 alpha 和 uninsatll 应用程序。清理,构建并再次安装。因为它对我有用
  • 我不想移除 alpha - 图像应该使用 alpha 0.2 淡化。应用程序正在做一些事情,什么时候准备好我想让我的图像动画到 alpha 1。
  • 请检查已编辑的代码在我的 google nexus 4 上的工作正常。我的 imageview alpha 在开始时为 0.2,但在应用动画后它更改为 1.0 并且更改仍然存在。
  • 我采用了不同的方法——我没有在布局中设置 alpha 或通过 setAlpha/setImageAlpha 设置动画 0.2->1 并将其放在 Create 上。现在,当我的动画被触发时,它完美地从 0.2->1
【解决方案2】:
<ImageView
    android:id="@+id/listViewIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/settings"/>  

从 XML-> ImageView 中删除 android:alpha=0.2

【讨论】:

    【解决方案3】:

    嗯……

    事情是错误的,可能在Android API中动画的正确操作。

    事实是,当您在代码中设置 alpha 值 0.2f 时,它是基于 xml 文件中针对 android 的设置,这意味着:

    0.2f = 0.2f of 0.2f (20% from 100%) ie from 0.2f / 5 = 0.04f
    1f = 0.2f
    

    所以你的动画实际上从 0.04f 到 0.2f

    flag setFillAfter(true) 确实有效,但您需要了解,在动画结束时 ImageView 将具有 alpha 值 0.2f 而不是 1,因为您将 0.2f 指定为边缘动画中可接受的值(一种最大的 Alpha 通道)。

    因此,如果您想获得所需的结果,应将所有逻辑带到您的代码中,并在代码中操作动画,而不是在 xml 中确定。

    您应该明白,您的动画直接取决于两件事:

    • 动画视图的LayoutParams
    • 动画参数。

    动画参数在 setFillAfter\setFillBefore 方法中操作您的 LayoutParams。

    【讨论】:

    • 是的。你说的对。当我在我的代码中更改开始布局参数时,它就像一个魅力!
    【解决方案4】:

    在开始动画之前将 alpha 设置为 1 对我有用:

    AlphaAnimation animation1 = new AlphaAnimation(0.2f, 1.0f);
    animation1.setDuration(500);
    iv.setAlpha(1f);
    iv.startAnimation(animation1);
    

    至少在我的测试中,没有闪烁,因为在开始动画之前设置了 alpha。它工作正常。

    【讨论】:

    • 这对我也有用(编辑:我的错误,我确实有 XML 干扰:android:alpha="0")。会玩这个,但我确实需要它从 0 开始,所以现在可以。谢谢。
    【解决方案5】:

    setStartOffset”应该更小,否则动画从视图 alpha 0.xf 开始,并在动画到 1f 之前等待开始偏移。希望下面的代码有所帮助。

    AlphaAnimation animation1 = new AlphaAnimation(0.1f, 1f);
    
    animation1.setDuration(1000);
    animation1.setStartOffset(50);
    
    animation1.setFillAfter(true);
    
    view.setVisibility(View.VISIBLE);
    
    view.startAnimation(animation1);
    

    【讨论】:

      【解决方案6】:

      可能有点晚了,但在 android 文档中找到了 lovely solution

      //In transition: (alpha from 0 to 0.5)
      view.setAlpha(0f);
      view.setVisibility(View.VISIBLE);
      view.animate()
         .alpha(0.5f)
         .setDuration(400)
         .setListener(null);
      
      //Out transition: (alpha from 0.5 to 0)
      view.setAlpha(0.5f)
      view.animate()
         .alpha(0f)
         .setDuration(400)
         .setListener(new AnimatorListenerAdapter() {
                 @Override
                 public void onAnimationEnd(Animator animation) {
                 view.setVisibility(View.GONE);
               }
          });
      

      【讨论】:

      • 这就是我要找的东西
      • 效果很好,谢谢!只是想知道,.setListener(null); 是否可能会删除/干扰其他听众,例如onClick/onTouch 监听器?
      【解决方案7】:

      Kotlin Version

      像这样简单地使用ViewPropertyAnimator

      iv.alpha = 0.2f
      iv.animate().apply {
          interpolator = LinearInterpolator()
          duration = 500
          alpha(1f)
          startDelay = 1000
          start()
      }
      

      【讨论】:

      • 当尝试将 beginDelayedTransition 与 ImageView.animate alpha 动画混合时,这是唯一可行的解​​决方案。
      • 耶!很好的答案! Kotlin 万岁!
      【解决方案8】:
      View.setOnTouchListener { v, event ->
          when (event.action) {
              MotionEvent.ACTION_DOWN -> {
                  v.alpha = 0f
                  v.invalidate()
              }
              MotionEvent.ACTION_UP -> {
                  v.alpha = 1f
                  v.invalidate()
      
      
              }
          }
          false
      }
      

      【讨论】:

        【解决方案9】:

        这是我的扩展,这是使用 FadIn 和 FadOut 更改图像的示例:

        fun ImageView.setImageDrawableWithAnimation(@DrawableRes() resId: Int, duration: Long = 300) {    
            if (drawable != null) {
                animate()
                    .alpha(0f)
                    .setDuration(duration)
                     .withEndAction {
                         setImageResource(resId)
                         animate()
                             .alpha(1f)
                             .setDuration(duration)
                     }
        
            } else if (drawable == null) {
                setAlpha(0f)
                setImageResource(resId)
                animate()
                    .alpha(1f)
                    .setDuration(duration)
            }
        }
        

        【讨论】:

          【解决方案10】:
          fun View.toggleAlpha(isShow: Boolean, delay: Long = 200, invisibleMode: Int = View.GONE) {
              if (isShow) animateAlpha(View.VISIBLE, delay) else animateAlpha(invisibleMode, delay)
          }
          
          fun View.animateAlpha(visibility: Int, delay: Long = 200) {
              if (visibility == View.VISIBLE) {
                  setVisibility(View.VISIBLE)
              }
          
              val alpha = when (visibility) {
                  View.GONE, View.INVISIBLE -> 0f
                  View.VISIBLE -> 1f
                  else -> 1f
              }
          
              animate().apply {
                  duration = delay
                  alpha(alpha)
                  withEndAction {
                      setVisibility(visibility)
                  }
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-06-01
            • 1970-01-01
            • 1970-01-01
            • 2011-03-18
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多