无论如何以编程方式为线性布局的布局权重属性设置动画

时间:2012-07-16 14:24:08

标签: android android-layout android-linearlayout android-animation

我在线性布局中有两个视图,我以编程方式更改其layout_weight属性。有没有办法可以为这个重量变化设置动画,这样当重量变化时,视图会滑向新的尺寸?

5 个答案:

答案 0 :(得分:30)

您只需使用ObjectAnimator。

ObjectAnimator anim = ObjectAnimator.ofFloat(
                viewToAnimate,
                "weight",
                startValue,
                endValue);
        anim.setDuration(2500);
        anim.start();

一个问题是View类没有setWeight()方法(ObjectAnimator需要)。为了解决这个问题,我写了一个简单的包装器,它有助于归档视图权重动画。

public class ViewWeightAnimationWrapper {
    private View view;

    public ViewWeightAnimationWrapper(View view) {
        if (view.getLayoutParams() instanceof LinearLayout.LayoutParams) {
            this.view = view;
        } else {
            throw new IllegalArgumentException("The view should have LinearLayout as parent");
        }
    }

    public void setWeight(float weight) {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
        params.weight = weight;
        view.getParent().requestLayout();
    }

    public float getWeight() {
        return ((LinearLayout.LayoutParams) view.getLayoutParams()).weight;
    }
}

以这种方式使用它:

    ViewWeightAnimationWrapper animationWrapper = new ViewWeightAnimationWrapper(view);
    ObjectAnimator anim = ObjectAnimator.ofFloat(animationWrapper,
                    "weight",
                    animationWrapper.getWeight(),
                    weight);
            anim.setDuration(2500);
            anim.start();

答案 1 :(得分:4)

我也一直在关注这个问题。最终我通过动画父级的权重属性来解决它,如果你在LinearLayout中有两个视图,那就非常好。

请参阅: Animating weightSum property using ObjectAnimator

在下面的示例中,如果您将weightSum从1.0设置为2.0,则屏幕2将很好地显示在视图中。

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

答案 2 :(得分:3)

注意:我不确定这是最好的方式,但我尝试了它并且工作正常

只需使用ValueAnimator

即可
ValueAnimator m1 = ValueAnimator.ofFloat(0.2f, 0.5f); //fromWeight, toWeight
m1.setDuration(400);
m1.setStartDelay(100); //Optional Delay
m1.setInterpolator(new LinearInterpolator());
m1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
         @Override
         public void onAnimationUpdate(ValueAnimator animation) {
             ((LinearLayout.LayoutParams) viewToAnimate.getLayoutParams()).weight = (float) animation.getAnimatedValue();
             viewToAnimate.requestLayout();
         }

});
m1.start();

More About ValueAnimator

答案 3 :(得分:0)

另一种方法是使用旧的Animation类,如https://stackoverflow.com/a/20334557/2914140中所述。在这种情况下,您可以同时更改多个视图的权重。

private static class ExpandAnimation extends Animation {
    private final View[] views;
    private final float startWeight;
    private final float deltaWeight;

    ExpandAnimation(View[] views, float startWeight, float endWeight) {
        this.views = views;
        this.startWeight = startWeight;
        this.deltaWeight = endWeight - startWeight;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        float weight = startWeight + (deltaWeight * interpolatedTime);
        for (View view : views) {
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) view.getLayoutParams();
            lp.weight = weight;
            view.setLayoutParams(lp);
        }
        views[0].getParent().requestLayout();
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }
}

答案 4 :(得分:0)

上面的所有答案都不适合我(它们只是“捕捉”而不是动画),但是在我将weight_sum =“ 1”添加到父布局后,它开始起作用。以防万一有人提出相同的问题。