片段动画,如Gmail Honeycomb App

时间:2011-09-07 19:27:42

标签: android android-layout android-fragments android-animation

如何在Android 3.0 +上的Gmail应用中重新创建布局动画效果

我知道你可以使用PropertyAnimators,ObjectAnimators等,但在我的场景中,我在屏幕上有几个可以互换的片段,所以它们都包含在自己的FrameLayouts中,并且所有的FrameLayouts都在一个RelativeLayout。

我无法弄清楚如何更改FrameLayouts的宽度,因为您必须通过LayoutParams对象进行编辑。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我对此的看法可以查看on GitHub here

它展示了一种用动画隐藏片段的方法,就像Gmail一样。

动画并不完美,因为左片段的移动速度比右片段快一点。

执行隐藏的方法在代码中定义动画,如下所示:

/**
 * Besides showing/hiding the left fragment, this method specifies that a
 * layout animation should be used. It is defined as a sliding animation.
 * The right fragment will use the default animation, which is a sliding
 * animation also.
 * 
 * If the left fragment's animation is removed from this method, the default
 * animation will be used which is a fading animation.
 * 
 * Please note that this method will only have an effect in those screen
 * configurations where the list is hideable; by default, a width between
 * 600 and 1024 dip which corresponds to a portrait view on tablets. Change
 * the boolean value in layout_constants.xml to allow for it in other screen
 * sizes.
 * 
 * @param visible
 */
protected void setLeftFragmentVisible(boolean visible) {
    if (leftFragment != null && (leftFragment.isVisible() || visible)
            && getResources().getBoolean(R.bool.leftHideable)) {
        final float listWidth = getLeftFragment().getView().getWidth();
        ViewGroup container = (ViewGroup) findViewById(R.id.dual_layout);
        // Don't clip the children, we want to draw the entire fragment even
        // if it is partially off-screen.
        container.setClipChildren(false);
        final LayoutTransition trans = container.getLayoutTransition();
        /**
         * This specifies the delay before the leftFragment will appear.
         * Change if you want the right fragment to move before.
         */
        trans.setStartDelay(LayoutTransition.APPEARING, 0);
        /**
         * This is the delay before the right fragment will start to occupy
         * the space left by the left fragment
         */
        trans.setStartDelay(LayoutTransition.CHANGE_DISAPPEARING, 100);

        /**
         * Adding, specifies that the left fragment should animate by
         * sliding into view.
         */
        ObjectAnimator animIn = ObjectAnimator.ofFloat(null, "x",
                -listWidth, 0f).setDuration(
                trans.getDuration(LayoutTransition.CHANGE_APPEARING));
        trans.setAnimator(LayoutTransition.APPEARING, animIn);

        /**
         * Removing, specifies that the left fragment should animate by
         * sliding out of view.
         */
        ObjectAnimator animOut = ObjectAnimator.ofFloat(null, "x", 0f,
                -listWidth).setDuration(
                trans.getDuration(LayoutTransition.CHANGE_DISAPPEARING));
        trans.setAnimator(LayoutTransition.DISAPPEARING, animOut);

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();
        if (getLeftFragment().isVisible()) {
            fragmentTransaction.hide(getLeftFragment());
        } else {
            fragmentTransaction.show(getLeftFragment());
        }
        // The hiding/showing will automatically initiate the animations
        // since
        // we have specified that we want layout animations in the layout
        // xml
        fragmentTransaction.commit();

        /*
         * Display home as up to be able to view the list
         */
        getActionBar().setDisplayHomeAsUpEnabled(!visible);
    }
}

要使其工作,您需要定义要对布局转换进行动画处理。布局顶部xml结构中的一行将执行此操作:

android:animateLayoutChanges="true"

如果您不想要自定义动画,可以在FragmentManager fragmentManager = getFragmentManager()之前删除所有内容。

现在在我的例子中我使用片段,但同样的原则应该适用于任何视图。

编辑:不应手动更改视图的宽度,而应使用布局权重来自动调整大小。