gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:18:55 25 4
gpt4 key购买 nike

我的平板电脑应用程序有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com