Android 在片段之间滑动左/右动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20802369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:29:31  来源:igfitidea点击:

Slide left/right animation between fragments

androidanimationandroid-fragments

提问by ludriv

I post a question here because I can't find out a solution to my problem. I read a lot of things about android animation.

我在这里发布了一个问题,因为我找不到解决我的问题的方法。我读了很多关于android动画的东西。

I actually develop an android 4.0 app and I need to animate transition between fragments (not layout).

我实际上开发了一个 android 4.0 应用程序,我需要动画片段之间的过渡(不是布局)。

Similary post, worked with layout but no more precision about fragments

类似的帖子,使用布局但没有关于片段的更精确

Here my uncomplete code :

这是我不完整的代码:

Activity code

活动代码

private void showFragment(final Fragment fragment)
{
    if (null == fragment)
        return;

    FragmentTransaction ft = getFragmentManager().beginTransaction();

    ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);

    ft.replace(R.id.fragment_container_layout, fragment, fragment.getClass().getSimpleName()).commit();

}

R.anim.slide_in_left

R.anim.slide_in_left

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="@android:integer/config_mediumAnimTime">

    <translate
        android:fromXDelta="100%p"
        android:toXDelta="0" />

    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />

</set>

And finally R.anim.slide_out_right

最后是R.anim.slide_out_right

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_mediumAnimTime" >

    <translate
        android:fromXDelta="0"
        android:toXDelta="-100%p" />

    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />

</set>

When I run this code, I got an exception : 12-27 15:26:55.566: E/AndroidRuntime(27699): java.lang.RuntimeException: Unknown animator name: translate

当我运行此代码时,出现异常: 12-27 15:26:55.566: E/AndroidRuntime(27699): java.lang.RuntimeException: Unknown animator name: translate

Do you have any idea to fix this ?

你有什么想法来解决这个问题吗?

采纳答案by ludriv

Problem solved!

问题解决了!

Like Piyush Gupta said, I must have a custom subclass of FrameLayoutper FragmentI need to animate, first.

就像 Piyush Gupta 所说的那样,首先我必须有一个自定义的子类FrameLayoutper FragmentI 需要动画。

Secondly, I must not use R.animbut R.animatorlike another similar post (link in question).

其次,我不能使用R.animR.animator喜欢另一个类似的帖子(有问题的链接)。

Thank you all !

谢谢你们 !