从XML加载片段时动画片段转换

hoo*_*dfx 5 android android-layout android-fragments android-3.0-honeycomb

我的平板电脑应用程序针对不同的UI模式有一个活动和几个不同的布局 - 这些布局中的每一个都使用<fragment>标签来填充具有不同片段的UI(在Activity切换模式中调用setContentView).

如何以这种方式加载新片段时,如何使用过渡动画淡入?现在,在片段加载时,在模式之间切换会产生闪烁效果.

谢谢!

Jam*_*meo 2

我以前从未使用过片段,但是片段没有理由会影响我的解决方案。基本上,您实现了一个要在某些内容的第一个布局上显示的动画。最好的例子是列表视图

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

布局控制器.xml:

<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="50%"
android:animation="@anim/bounce" />
Run Code Online (Sandbox Code Playgroud)

这定义了布置某些东西的过程。
然后,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"
    />
Run Code Online (Sandbox Code Playgroud)

该动画将使项目弹入,同时淡入。

现在,如果您有一个列表视图,请将其设置在它的 XML 中(适用于文本视图、图像视图等)

<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:persistentDrawingCache="animation|scrolling"
android:layoutAnimation="@anim/layout_controller"
/>
Run Code Online (Sandbox Code Playgroud)

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