使用 ObjectAnimator 对 WeightSum 属性进行动画处理
简介:
我有一个 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>
最初,我希望“Screen 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 与切换可见性相结合,但无济于事
Intro:
I have a LinearLayout, which contains two sub LinearLayouts, like so:
<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>
Initially, I want "Screen 1" to take all screen width available. Therefore, my R.id.dual_pane has it's weightSum attribute to 1.0. This works fine! if weightSum=1.0, Screen 1 occupies the whole screen!
After loading some resources, I change my R.id.dual_pane weighSum to 2.0, which results in both Screen 1 and Screen 2 taking 50% off the width of the screen. This also works perfect. When weightSum=2.0, both screens take 50% of the width.
Problem:
I would like to animate the weightSum property, so my Screen2 will slide in.
I am targeting HoneyComb, so minSDK version is 11, and I figured, using the new ObjectAnimator framework, I could easily animate this property, to get a nice smooth effect. I verified that LinearLayout indeed has getWeightSum() and setWeightSum() methods (which is required to use the ObjectAnimator, I think).
Own effort:
Here's my code to showing and hiding Screen2 using the ObjectAnimator :
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();
}
Here, my mDualPane is my root LinearLayout...
Question:
When I call these functions, nothing happens. The screen stays exactly like it was before.
Do I need to call requestLayout() on my mDualPane somewhere? Am I missing some knowledge of the ObjectAnimator? Or is it impossible to animate the weightSum property?
ALSO:
1) I don't want to mess with hard-coded widths, and animate those. For now I want 50-50 for both screens, but I might change it later. Anyway, i need to be able to set a specific ratio between the two widths.
2) I have looked at LayoutTransition combined with toggling visibility, but to no avail
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我是对的,我需要自己更新布局:
现在,我向 ObjectAnimator 添加了一个 UpdateListener,它由我的 Activity 实现并更新布局:
对我来说,ObjectAnimator 本身并不调用它,这似乎很奇怪,但无论如何,这就是让它发挥作用的方法。
在我看来,该解决方案特别好,因为您可以很好地为滑入的布局设置动画,而与屏幕尺寸无关......
I was right in the sense that I need to update the layout myself:
Now, I added an UpdateListener to the ObjectAnimator, which is implemented by my Activity and updates the layout:
It seems strange to me, that ObjectAnimator doesn't call this itself, but anyway, this is how to get it working.
The solution is especially nice, in my opinion, since you can very nicely animate layouts sliding in, independent of screensize...