Android:我的片段过渡动画出了什么问题?

发布于 2024-11-06 12:09:38 字数 1199 浏览 1 评论 0原文

我只需要简单的滑入和滑出动画来进行片段过渡,下面是我的代码: Slide_in_left.xml:

<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true">
<translate
    android:fromXDelta="-100%"
    android:toXDelta="0%"
    android:fromYDelta="0%"
    android:toYDelta="0%"
    android:duration="700">
</translate>
</set>

slide_out_right.xml:

<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
    android:fromXDelta="0%" android:toXDelta="100%"
    android:fromYDelta="0%" android:toYDelta="0%"
    android:duration="700">
</translate>
</set>

我使用的代码:

SomeFragment frag = SomeFragment.newInstance(foo);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
ft.replace(R.id.the_fragment, frag);
ft.addToBackStack(null);

ft.commit();

结果看起来很奇怪,当过渡开始时,当前片段消失而没有动画,进入的片段(从左侧)像滚动纸一样。我的动画 xml 代码有什么问题?

谢谢!

I just need plain slide in and slide out animation for Fragment transition, below is my code:
slide_in_left.xml:

<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true">
<translate
    android:fromXDelta="-100%"
    android:toXDelta="0%"
    android:fromYDelta="0%"
    android:toYDelta="0%"
    android:duration="700">
</translate>
</set>

slide_out_right.xml:

<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
    android:fromXDelta="0%" android:toXDelta="100%"
    android:fromYDelta="0%" android:toYDelta="0%"
    android:duration="700">
</translate>
</set>

the code I used:

SomeFragment frag = SomeFragment.newInstance(foo);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
ft.replace(R.id.the_fragment, frag);
ft.addToBackStack(null);

ft.commit();

The result looks very stranges, when the transition starts, the current fragment disappears without animation, the entering fragment comes(from left) like a scrolling paper. What's wrong with my animation xml code?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千と千尋 2024-11-13 12:09:38

我猜 getSupportFragmentManager() 意味着您正在使用片段兼容包。
我也有同样的问题,根本没有动画发生。

请参阅 http://groups.google.com/group/android -开发者/browse_thread/thread/5ef5ba1be9f40c56/a846578d91a032c0?hide_quotes=yes#msg_8ca017c473818a04

getSupportFragmentManager() means you are using the Compatibility Package for Fragments, I guess.
I have the same problem, which is no animation happening at all.

See http://groups.google.com/group/android-developers/browse_thread/thread/5ef5ba1be9f40c56/a846578d91a032c0?hide_quotes=yes#msg_8ca017c473818a04

心如狂蝶 2024-11-13 12:09:38

Google 已更新兼容性库,并且转换已修复。你们应该从 android sdk/avd 管理器更新您的兼容性库。

Google has updated the compatibility library and the transitions have been fixed. You guys should update your compatiblity library from the android sdk/avd manager.

衣神在巴黎 2024-11-13 12:09:38

我在这里找到了一篇关于片段兼容性库的很酷的帖子动画问题,我采取了第二种方法

..... 调用 FragmentTransaction.setCustomAnimations(),引用动画师或动画(取决于您是否使用
是否兼容库)。有趣的是
setCustomAnimations() 影响添加到的所有片段过渡
调用后的事务。所以你需要打电话
setCustomAnimations() 在你想要使用它之前,你实际上可以
为每个部分设置多个不同的自定义动画
事务(在每个事务之前调用 setCustomAnimations()
添加()/删除()/附加()/分离()/显示()/隐藏()/替换())......

像这样

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.push_up_in,R.anim.push_up_out);
ft.commit();
ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.push_up_in,R.anim.push_up_out);
if (fragment1.isHidden()) {
ft.show(fragment1);
} else {
ft.hide(fragment1);

}
ft.commit();

我解决了api level 11的问题希望它能工作!

I found a cool post here about the Fragment Compatibility library animations issue, i took second approach

..... Call FragmentTransaction.setCustomAnimations(), referencing either animators or animations (depending on whether you're using the
compatibility library or not). What is interesting is that
setCustomAnimations() affects all fragment transitions added to the
transaction after it is called. So you need to call
setCustomAnimations() before you want it used, and you can actually
setup multiple different custom animations for each part of a
transaction (with a call to setCustomAnimations() before each
add()/remove()/attach()/detach()/show()/hide()/replace())......

like this

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.push_up_in,R.anim.push_up_out);
ft.commit();
ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.push_up_in,R.anim.push_up_out);
if (fragment1.isHidden()) {
ft.show(fragment1);
} else {
ft.hide(fragment1);

}
ft.commit();

With this i solved the problem for api level 11 hope it works!

水水月牙 2024-11-13 12:09:38

如果您使用 ViewPager

mPager.setOffscreenPageLimit(YourFragmentsSize); 

这一行解决了我的问题,希望也适合您。 引用自本主题

If you're using ViewPager

mPager.setOffscreenPageLimit(YourFragmentsSize); 

This line solved my problem, hope works for you too. Referanced from this topic

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文