android - 从 XML 加载 fragment 时动画 fragment 转换

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

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

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

谢谢!

最佳答案

我以前从未使用过 fragment ,但没有理由说 fragment 会影响我的解决方案。基本上,您实现一个动画以显示在某物的第一个布局上。最好的例子是 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"
    />

此动画会弹回项目,同时也会淡入。

现在如果你有一个 ListView ,在它的 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 引用​​布局 Controller 如何显示 ListView 。首次绘制 ListView 时,每个项目都应依次反弹。您可以通过更改 bounce.xml 轻松自定义动画,或通过更改 layout_controller 中定义的 50% 延迟来更改等待时间。

关于android - 从 XML 加载 fragment 时动画 fragment 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8580469/

相关文章:

Android NDK r10: std::string 尚未声明

android - MVVMCross 按钮命令绑定(bind)未触发

android - 降低背景层的对比度和亮度

android - 找到屏幕尺寸后更改 FrameLayout 的尺寸

android - 平板电脑上 Android Honeycomb 的 UI 指南

Android Honeycomb : How to determine when FragmentTrancaction. commit() 已经完成?

android - 二进制 XML 文件第 9 行 : Error inflating class fragment

android - 无法实例化 Activity ComponentInfo ... java.lang.NullPointerException

java - 我正在尝试使用 ListView 按钮从 cutomlistadapter 打开 Android Activity 。我尽了最大努力但没有结果?

安卓性能 : Adding view programmatically vs setting view to GONE/VISIBLE