【问题标题】:Animate fragment transition when fragments are loaded from XML从 XML 加载片段时的动画片段过渡
【发布时间】:2011-12-20 18:53:43
【问题描述】:

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

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

谢谢!

【问题讨论】:

  • 您是否尝试在 布局中加载不同的片段?

标签: android android-layout android-3.0-honeycomb android-fragments


【解决方案1】:

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

首先,您需要添加几个额外的动画文件,添加到 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"
/>

layoutAnimation 字段告诉列表视图参考​​布局控制器如何显示列表视图。当listview第一次被绘制时,每个item都应该依次反弹进来。你可以通过改变bounce.xml轻松自定义动画,或者通过改变layout_controller中定义的50%延迟来改变等待时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    相关资源
    最近更新 更多