当 alpha 已在视图中设置时,为什么 alpha 动画不起作用
Why alpha animation doesn't work when alpha is already set at view
我发现了一个奇怪的情况。下面的 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
行一起工作
尝试使用下面的代码
((ImageView)getActivity().findViewById(R.id.img)).animate().alpha(1f).setDuration(3000).start();
希望对您有所帮助
我发现了一个奇怪的情况。下面的 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
尝试使用下面的代码
((ImageView)getActivity().findViewById(R.id.img)).animate().alpha(1f).setDuration(3000).start();
希望对您有所帮助