【问题标题】:Why alpha animation doesn't work when alpha is already set at view为什么在视图中已经设置了 alpha 时 alpha 动画不起作用
【发布时间】:2018-07-25 03:26:37
【问题描述】:

我发现了一个奇怪的情况。下面的animation.xml 不起作用。 ImageView 总是不可见的。

activity.xml

...
<ImageView
      android:layout_width="324px"
      android:layout_height="90px"
      android:alpha="0"
      android:src="@drawable/img"
      android:id="@+id/img"/>
...

custom_anim.xml(不可见到可见)

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fromAlpha="0.0"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:toAlpha="1.0"/>

MainActivity.java

Animation anim = AnimationUtils.loadAnimation(getContext(), R.anim.custom_anim);
...
...
((ImageView)getActivity().findViewById(R.id.img)).startAnimation(anim);

但是,如果删除activity.xml 上的android:alpha 行,动画将起作用。当然ImageView在动画工作之前不是不可见的,但至少可以工作。请让我知道为什么会发生这种情况以及如何使动画可以使用android:alpha

【问题讨论】:

    标签: android android-animation android-xml


    【解决方案1】:

    尝试使用下面的代码

    ((ImageView)getActivity().findViewById(R.id.img)).animate().alpha(1f).setDuration(3000).start();
    

    希望对你有帮助

    【讨论】:

    • 成功了!谢谢!如果你得到xml无法控制alpha值的原因,请给我cmets!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多