要实现这一点,你已经开始,你将需要添加一个
AnimationListener,以便您可以检测动画的开始和结束。当调用淡出的onAnimationEnd()时,您可以将ImageView对象的可见性设置为View.INVISIBLE,切换图像并开始淡入动画 – 您还需要另一个AnimationListener。当你收到onAnimationEnd()为你的淡入动画,设置ImageView为View.VISIBLE,这应该给你的效果,你正在寻找。
我之前已经实现了类似的效果,但我使用了一个ViewSwitcher与2 ImageViews而不是一个单一的ImageView。你可以设置“in”和“out”动画的ViewSwitcher与您的淡入和淡出,以便它可以管理AnimationListener实现。然后你需要做的是在2个ImageView之间交替。
编辑:
为了更有用,这里是如何使用ViewSwitcher的一个快速示例。我在https://github.com/aldryd/imageswitcher包括了完整的源代码。
activity_main.xml
android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:inAnimation="@anim/fade_in"
android:outAnimation="@anim/fade_out" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:src="@drawable/sunset" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:src="@drawable/clouds" />
MainActivity.java
// Let the ViewSwitcher do the animation listening for you
((ViewSwitcher) findViewById(R.id.switcher)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewSwitcher switcher = (ViewSwitcher) v;
if (switcher.getDisplayedChild() == 0) {
switcher.showNext();
} else {
switcher.showPrevious();
}
}
});