gpt4 book ai didi

android - 像Gmail Honeycomb App这样的 fragment 动画

转载 作者:太空狗 更新时间:2023-10-29 15:21:33 24 4
gpt4 key购买 nike

如何重新创建布局动画影响您在 Android 3.0+ 上的 Gmail 应用程序中看到

我知道您可以使用 PropertyAnimators、ObjectAnimators 等,但在我的场景中,屏幕上有几个可以互换的 fragment ,因此它们都包含在自己的 FrameLayout 中,并且所有 FrameLayout 都在一个 RelativeLayout。

我不知道如何更改 FrameLayouts 的宽度,因为您必须通过 LayoutParams 对象对其进行编辑。

有什么想法吗?

最佳答案

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

它演示了一种使用动画隐藏 fragment 的方法,就像 Gmail 那样。

动画并不完美,因为左侧 fragment 的移动速度略快于右侧 fragment 。

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

/**
* 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() 之前的所有内容。

现在在我的示例中我使用了 fragment ,但同样的原则应该适用于任何 View 。

编辑:与其手动更改 View 的宽度,不如使用布局权重来允许自动调整大小。

关于android - 像Gmail Honeycomb App这样的 fragment 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7339498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com