使用动画ObjectAnimator weightSum财产财产、动画、ObjectAnimator、weightSum

2023-09-04 23:24:31 作者:愿时光盗不走你

简介:

我有一个LinearLayout中,其中包含两个子LinearLayouts,像这样:

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>

起初,我想屏幕1采取所有可用的屏幕宽度。因此,我R.id.dual_pane有它的weightSum属性为1.0。这工作得很好!如果weightSum = 1.0,画面1占满整个屏幕!

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!

加载一些资源后,我改变我的R.id.dual_pane weighSum到2.0,这会导致两个屏幕1和屏幕2以50%的折扣屏幕的宽度。这也可以完美运行。当weightSum = 2.0,两个屏幕取宽度的50%。

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.

问题:

我想动画的weightSum属性,所以我的画面2将下滑。 我瞄准蜂窝,所以minSDK的版本是11,我算了一下,使用新的ObjectAnimator框架,我可以很容易动画这个属性,得到一个不错的流畅的效果。我证实的LinearLayout确实有getWeightSum()和setWeightSum()方法(这是需要使用ObjectAnimator,我认为)。

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).

自己的努力:

下面是我的code,以显示和隐藏屏幕2使用ObjectAnimator:

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();
}

下面,我mDualPane是我的根的LinearLayout ...

Here, my mDualPane is my root LinearLayout...

问:

当我调用这些函数,没有任何反应。屏幕一直酷似以前。 我是否需要调用requestLayout()在我的mDualPane地方?我错过了ObjectAnimator的一些知识?抑或是不可能的weightSum物业动画?

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?

同时:

1)我不想惹硬codeD宽度,和动画的。现在我想50-50两个屏幕,但我以后可能会改变它。无论如何,我需要能够设置两种宽度之间的特定比率

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)我已经看过LayoutTransition结合切换的知名度,但都无济于事。

2) I have looked at LayoutTransition combined with toggling visibility, but to no avail

推荐答案

我是正确的,我需要更新布局自己的感觉:

I was right in the sense that I need to update the layout myself:

float ws = mDualPane.getWeightSum();
ObjectAnimator anim = ObjectAnimator.ofFloat(mDualPane, "weightSum", ws, 2.0f);
anim.setDuration(5000);
anim.addUpdateListener(this);
anim.start();

现在,我添加了一个UpdateListener到ObjectAnimator,这是我的活动实施和更新的布局:

Now, I added an UpdateListener to the ObjectAnimator, which is implemented by my Activity and updates the layout:

@Override
public void onAnimationUpdate(ValueAnimator animation) {
    mDualPane.requestLayout();
}

我觉得奇怪,我,说ObjectAnimator不叫这个本身,但无论如何,这是怎么得到它的工作。

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...