在通过动画的活动交换片段片段、动画

2023-09-12 03:48:10 作者:柒冉

我要交换两个片段活动通过animation.Suppose pageA的是fragement A和左侧的屏幕上,页面B是B片段,即在屏幕右侧的。现在我想,当我点击pageA的一个按钮,然后pageA的将移动到屏幕右侧的一些过渡动画。

I want to swap two fragment in an activity via animation.Suppose PageA is for fragement A and left side on the screen and PageB is for fragment B i.e. on the right side of the screen. Now i want that when i click a button on pageA then PageA will move to the right side of the screen with some transition animation.

我想下面的code来代替位置

I tried the below code to replace the position

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new FragB());
fragmentTransaction.commit();

寻找一些线索。

Looking for some clue.

在此先感谢。

推荐答案

旧questiion,你可能已经想通了,但是为了将来的参考:

Old questiion and you probably already figured it out, but for future reference:

这里是你用什么来设置自定义动画,当你通过code替换片段:

here's what you use to set a custom animation when you replace a fragment via code:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
ft.replace(R.id.fragment_container, newFragment, "fragment");
// Start the animated transition.
ft.commit();

下面是slide_in_left动画的示例:

Here is an example of the slide_in_left animation:

<?xml version="1.0" encoding="utf-8"?>
<set>
  <translate xmlns:android="http://schemas.android.com/apk/res/android"
   android:fromXDelta="-100%"
   android:toXDelta="0"
   android:interpolator="@android:anim/decelerate_interpolator"
   android:duration="500"/>
</set>

请注意,这是动画,如果你正在使用兼容性库。相反,如果你正在使用和SDK与用于FragmentManager原生支持那么你的动画将是这样的:

Note that this is the animation if you are using the compatibility library. Instead if you are using and SDK with native support for the FragmentManager then your animation will look like this:

<?xml version="1.0" encoding="utf-8"?>
<set>
  <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="x" 
    android:valueType="floatType"
    android:valueFrom="-1280"
    android:valueTo="0" 
    android:duration="500"/>
</set>

这是因为兼容性库不支持新的objectAnimator类型,而是只执行旧动画框架。

This is because the compatibility library does not support the new objectAnimator type and instead only implement the old animation framework.

 
精彩推荐
图片推荐