从XML加载片段时如何使片段转换动画?

5

我的平板应用程序只有一个活动和几个不同的布局,用于不同的UI模式 - 每个布局都使用<fragment>标记来使用不同的片段填充UI(在Activity中调用setContentView以切换模式)。

我该如何使用转换动画来淡入新的片段,当它们被这种方式加载时?目前,在模式之间切换会产生闪烁效果,因为片段正在加载。

谢谢!


你是否正在尝试在<fragment>布局中加载不同的片段? - Raneez Ahmed
1个回答

2

我以前从未使用过片段(fragments),但是拥有片段不会影响我的解决方案。基本上,您需要在某个布局的第一个层次上实现要显示的动画效果。最好的例子是Listview。

首先,您需要添加一些额外的动画文件,这些文件添加到res/anim中。

layout_controller.xml:

<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="50%"
android:animation="@anim/bounce" />

这定义了一种布局的过程。
然后,bounce.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/bounce_interpolator">
<translate
    android:fromXDelta="40%"
    android:toXDelta="0%"
    android:fromYDelta="0%"
    android:toYDelta="0%" 
    android:duration="900"/>
<alpha
    android:fromAlpha="0"
    android:toAlpha="1"
    android:duration="1000"
    android:interpolator="@android:anim/linear_interpolator"
    />

这个动画会让物品弹入并淡入。

如果你有一个列表视图,在它的XML中设置这个(对于TextView、ImageView等也适用)。

<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:persistentDrawingCache="animation|scrolling"
android:layoutAnimation="@anim/layout_controller"
/>

布局动画字段告诉列表视图在显示列表视图时参考布局控制器。当首次绘制列表视图时,每个项目应该依次弹跳。您可以通过更改bounce.xml来轻松自定义动画,或通过更改布局控制器中定义的50%延迟来更改等待时间。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接