为什么在已在视图中设置alpha时alpha动画不起作用

问题描述 投票:0回答:1

我发现了一个奇怪的情况。下面的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"/>

main activity.Java

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

但是,如果删除android:alpha上的行activity.xml,动画工作。当然,在动画制作之前,ImageView并不是看不见的,但至少它可以奏效。请让我知道为什么它发生了,我怎么能使动画可以使用线android:alpha

android android-animation android-xml
1个回答
1
投票

尝试使用下面的代码

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

希望这可以帮助

© www.soinside.com 2019 - 2024. All rights reserved.