【发布时间】:2011-12-01 12:55:50
【问题描述】:
简介:
我有一个 LinearLayout,其中包含两个子 LinearLayout,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dual_pane"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:weightSum="1.0">
<!-- Screen 1 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#ff0000"
android:layout_weight="1">
</LinearLayout>
<!-- Screen 2 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#ff6600"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
最初,我希望“屏幕 1”占用所有可用的屏幕宽度。因此,我的 R.id.dual_pane 的 weightSum 属性为 1.0。这很好用!如果weightSum=1.0,则屏幕1占据整个屏幕!
加载一些资源后,我将 R.id.dual_pane weightSum 更改为 2.0,这导致屏幕 1 和屏幕 2 的屏幕宽度都减少了 50%。这也很完美。当 weightSum=2.0 时,两个屏幕都占宽度的 50%。
问题:
我想为 weightSum 属性设置动画,这样我的 Screen2 就会滑入。 我的目标是 HoneyComb,所以 minSDK 版本是 11,我想,使用新的 ObjectAnimator 框架,我可以轻松地为这个属性设置动画,以获得很好的平滑效果。我验证了 LinearLayout 确实有 getWeightSum() 和 setWeightSum() 方法(我认为这是使用 ObjectAnimator 所必需的)。
自己的努力:
这是我使用 ObjectAnimator 显示和隐藏 Screen2 的代码:
private void showScreen2()
{
//Not-animated will work...
//mDualPane.setWeightSum(2.0f);
// Now try to animate the weightSum
float ws = mDualPane.getWeightSum();
ObjectAnimator anim = ObjectAnimator.ofFloat(mDualPane, "weightSum", ws, 2.0f);
anim.setDuration(5000);
anim.start();
}
private void hideScreen2()
{
//Not-animated will work...
//mDualPane.setWeightSum(1.0f);
// Now try to animate the weightSum
float ws = mDualPane.getWeightSum();
ObjectAnimator anim = ObjectAnimator.ofFloat(mDualPane, "weightSum", ws, 1.0f);
anim.setDuration(5000);
anim.start();
}
在这里,我的 mDualPane 是我的根 LinearLayout...
问题:
当我调用这些函数时,什么也没有发生。屏幕与以前完全一样。 我需要在某处调用 mDualPane 上的 requestLayout() 吗?我是否缺少一些有关 ObjectAnimator 的知识?还是无法为 weightSum 属性设置动画?
另外:
1) 我不想弄乱硬编码的宽度,并为它们设置动画。现在我想要两个屏幕都为 50-50,但我以后可能会改变它。无论如何,我需要能够设置两个宽度之间的特定比例。
2) 我查看了 LayoutTransition 与切换可见性的结合,但无济于事
【问题讨论】:
标签: android android-animation android-linearlayout