FragmentTransaction动画下​​滑超过最高 [英] FragmentTransaction animation to slide in over top

查看:112
本文介绍了FragmentTransaction动画下​​滑超过最高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用FragmentTransaction.setCustomAnimations达到以下效果。

  1. 在碎片A显示
  2. 替换片段A与B.片段一个片段应在更换过程中仍然可见。 B片段应该从右侧滑动。片段乙方应滑动OVER片段A的顶部。

我没有问题,得到的动画设定的幻灯片。我的问题是,我无法弄清楚如何使片段A留在当前位置,并下片段B的动画幻灯片运行时。无论我做什么,似乎片段A是在上面。

我怎样才能做到这一点?

下面是FragmentTransaction code:

  FragmentTransaction英尺= getSupportFragmentManager()的BeginTransaction()。
ft.setCustomAnimations(R.anim.slide_in_right,R.anim.nothing,R.anim.nothing,
    R.anim.slide_out_right);
ft.replace(R.id.fragment_content,片段名);
ft.addToBackStack(名称);
ft.commit();
 

正如你可以看到我已经定义了一个动画R.anim.nothing为走出去的动画,因为我其实不想片段A做任何事情比其他仅仅停留在它是交易过程中。

下面是动画资源:

slide_in_right.xml

 <翻译的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:时间=@安卓整数/ config_mediumAnimTime
    机器人:fromXDelta =100%P
    机器人:toXDelta =0
    机器人:zAdjustment =顶/>
 

nothing.xml

 <阿尔法的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:时间=@安卓整数/ config_mediumAnimTime
    机器人:fromAlpha =1.0
    机器人:toAlpha =1.0
    机器人:zAdjustment =底部/>
 

解决方案

我发现了一个解决方案,为我工作。最后我用了FragmentStatePagerAdapter一个ViewPager。该ViewPager提供刷卡的行为,并在片段FragmentStatePagerAdapter互换。最终招实现下的具有一个页可见的效果传入页是使用一个PageTransformer。该PageTransformer覆盖页之间的ViewPager的默认过渡。下面是一个例子PageTransformer,可实现与翻译和少量缩放左侧页上的效果。

 公共类ScalePageTransformer实现PageTransformer {
    私有静态最终浮动SCALE_FACTOR = 0.95f;

    私人最终ViewPager mViewPager;

    公共ScalePageTransformer(ViewPager viewPager){
            this.mViewPager = viewPager;
    }

    @燮pressLint(NewApi)
    @覆盖
    公共无效transformPage(查看页面,浮动位){
        如果(位置< = 0){
            //应用变焦效果,只有换页,以抵消翻译
            // 左边
            最终浮动transformValue = Math.abs(Math.abs(位置) -  1)*(1.0F  -  SCALE_FACTOR)+ SCALE_FACTOR;
            INT页宽= mViewPager.getWidth();
            最终浮动translateValue =位置* -pageWidth;
            page.setScaleX(transformValue);
            page.setScaleY(transformValue);
            如果(translateValue> -pageWidth){
                page.setTranslationX(translateValue);
            } 其他 {
                page.setTranslationX(0);
            }
        }
    }

}
 

I am trying to achieve the following effect using FragmentTransaction.setCustomAnimations.

  1. Fragment A is showing
  2. Replace Fragment A with Fragment B. Fragment A should remain visible during the replacement. Fragment B should slide in from the right. Fragment B should slide in OVER THE TOP of Fragment A.

I have no problem getting the slide in animation setup. My problem is that I cannot figure out how to make Fragment A stay where it is and be UNDER Fragment B while the slide in animation is running. No matter what I do it seems that Fragment A is on top.

How can I achieve this?

Here is the FragmentTransaction code:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_right, R.anim.nothing, R.anim.nothing,
    R.anim.slide_out_right);
ft.replace(R.id.fragment_content, fragment, name);
ft.addToBackStack(name);
ft.commit();

As you can see I have defined an animation R.anim.nothing for the "out" animation because I actually don't want Fragment A to do anything other than just stay where it is during the transaction.

Here are the animation resources:

slide_in_right.xml

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_mediumAnimTime"
    android:fromXDelta="100%p"
    android:toXDelta="0"
    android:zAdjustment="top" />

nothing.xml

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_mediumAnimTime"
    android:fromAlpha="1.0"
    android:toAlpha="1.0"
    android:zAdjustment="bottom" />

解决方案

I found a solution which works for me. I ended up using a ViewPager with a FragmentStatePagerAdapter. The ViewPager provides the swiping behavior and the FragmentStatePagerAdapter swaps in the fragments. The final trick to achieve the effect of having one page visible "under" the incoming page is to use a PageTransformer. The PageTransformer overrides the ViewPager's default transition between pages. Here is an example PageTransformer that achieves the effect with translation and a small amount of scaling on the left-hand side page.

public class ScalePageTransformer implements PageTransformer {
    private static final float SCALE_FACTOR = 0.95f;

    private final ViewPager mViewPager;

    public ScalePageTransformer(ViewPager viewPager) {
            this.mViewPager = viewPager;
    }

    @SuppressLint("NewApi")
    @Override
    public void transformPage(View page, float position) {
        if (position <= 0) {
            // apply zoom effect and offset translation only for pages to
            // the left
            final float transformValue = Math.abs(Math.abs(position) - 1) * (1.0f - SCALE_FACTOR) + SCALE_FACTOR;
            int pageWidth = mViewPager.getWidth();
            final float translateValue = position * -pageWidth;
            page.setScaleX(transformValue);
            page.setScaleY(transformValue);
            if (translateValue > -pageWidth) {
                page.setTranslationX(translateValue);
            } else {
                page.setTranslationX(0);
            }
        }
    }

}

这篇关于FragmentTransaction动画下​​滑超过最高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆