动画片段,如Gmail蜂窝应用 [英] Fragment Animation like Gmail Honeycomb App

查看:97
本文介绍了动画片段,如Gmail蜂窝应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能重新布局动画会影响你在Android 3.0中的Gmail应用看+

How can I recreate the layout animation affects you see in the Gmail app on Android 3.0+

我知道你可以用PropertyAnimators,ObjectAnimators等,但在我的情况,我在屏幕上的几个片段,可以互换,因此它们都包含自己的FrameLayouts内,所有的FrameLayouts都在一个RelativeLayout的。

I know you can use PropertyAnimators, ObjectAnimators, etc., but in my scenario, I have a several fragments on the screen which can be interchanged, so they're all contained within their own FrameLayouts, and all the FrameLayouts are in a RelativeLayout.

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

I can't figure out how to change the width of the FrameLayouts, since you have to edit that through the LayoutParams object.

任何想法?

推荐答案

我拿到这个在这里可以查看 GitHub上

My take on this can be viewed on GitHub here

这表明隐藏着一个片段,动画的方式之一,如Gmail一样。

It demonstrates one way of hiding a fragment with an animation, like Gmail does.

动画是不完美的,因为左片段稍快动比右片段。

The animation isn't perfect as the left fragment is moving slightly faster than the right fragment is.

这确实隐藏定义动画code和外观如下的方法:

The method that does the hiding defines the animation in code and looks as follows:

/**
 * 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结构的顶部一号线将做到这一点:

To make this work, you need to define that you want layout transition to be animated. One line in the top of your layout's xml structure will do that:

android:animateLayoutChanges="true"

如果您不希望自定义动画,可以在FragmentManager fragmentManager = getFragmentManager删除一切()。

If you don't want custom animations, you can remove everything before FragmentManager fragmentManager = getFragmentManager().

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

Now in my example I use fragments but the same principle should apply to any view.

编辑:除了手动改变你的观点的宽度,你应该使用布局的权重,而不是允许自动调整大小的

Instead of changing the widths of your views manually, you should use layout weights instead to allow automatic resizing.

这篇关于动画片段,如Gmail蜂窝应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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