缩放布局以匹配父级动画

时间:2015-08-16 16:59:48

标签: android android-animation

在我的示例项目中,我有一个线性布局,它的高度设置为在xml中包装内容,并且在它下面还有一个片段,它占用了所有剩余的空间。片段包含一个按钮,单击该按钮将删除片段,线性布局的高度设置为与父级匹配。我尝试添加android:animateLayoutChanges="true"但是从wrap_content到match_parent的转换并不顺利。我如何从android:layout_height="wrap_content"动画到android:layout_height="match_parent"

这是布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@drawable/bg"
          android:orientation="vertical"
          android:id="@+id/layoutRoot"
          tools:context=".MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:id="@+id/cloudHolder"
    android:orientation="vertical"
    android:animateLayoutChanges="true"
    android:gravity="center"
    android:layout_height="wrap_content">
    <ImageButton
        android:layout_width="100dp"
        android:layout_gravity="center"
        android:layout_height="100dp"
        android:background="@drawable/play"/>
</LinearLayout>

4 个答案:

答案 0 :(得分:1)

@tdjprog Answer with some edit

1 - 拉伸

    private void animateViewTostritch_height(final View target) {
        int fromValue = 0;
        // int fromValue = target.getHeight();

        // int toValue = ((View) target.getParent()).getHeight();// matchparent
        int toValue = (int) getResources().getDimension(R.dimen.dialog_header_height);//spesific hight
        // int toValue = (int) (getResources().getDimension(R.dimen.dialog_header_height) / getResources().getDisplayMetrics().density);

        ValueAnimator animator = ValueAnimator.ofInt(fromValue, toValue);

        animator.setDuration(2000);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                target.getLayoutParams().height = (int) animation.getAnimatedValue();
                target.requestLayout();
            }
        });

        animator.start();
    }

调用 animateViewTostritch_height(your_View);

2 - 比例 %

public void scaleView(View v, float startScale, float endScale) {
    Animation anim = new ScaleAnimation(
            1f, 1f, // Start and end values for the X axis scaling
            startScale, endScale, // Start and end values for the Y axis scaling
            Animation.RELATIVE_TO_SELF, 0f, // Pivot point of X scaling
            Animation.RELATIVE_TO_SELF, 1f); // Pivot point of Y scaling
    anim.setFillAfter(true); // Needed to keep the result of the animation
    anim.setDuration(1000);
    v.startAnimation(anim);
}

调用 scaleView(your_View,0f,10f); // 10f 匹配父级

答案 1 :(得分:0)

您可能需要尝试添加android:animateLayoutChanges =&#34; true&#34;在父布局本身,例如:

<LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@drawable/bg"
      android:orientation="vertical"
      android:animateLayoutChanges="true"
      android:id="@+id/layoutRoot"
      tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:id="@+id/cloudHolder"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_height="wrap_content">

        <ImageButton
            android:layout_width="100dp"
            android:layout_gravity="center"
            android:layout_height="100dp"
            android:background="@drawable/play"/>

    </LinearLayout>

</LinearLayout>

如果上述代码无效,您可能需要查看:

Animation of height of LinearLayout container with ValueAnimator

或者

Animation in changing LayoutParams in LinearLayout

答案 2 :(得分:0)

我认为依靠animateLayoutChanges并不是一个好主意。请尝试以下代码。

import android.view.ViewPropertyAnimator;

// In your activity
private int parentHeight;
private int childHeight;
private float childScaleFactor;


//In onCreate()
mChildView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        parentHeight = mParentView.getMeasuredHeight();
        childHeight = mChildView.getMeasuredHeight();
        childScaleFactor = parentHeight/childHeight;
    }
});


mChildView.animate()
    .scaleY(childScaleFactor)
    .setDuration(500)
    .start();

如果这不起作用,请参阅另一篇文章中的this answer

答案 3 :(得分:0)

尝试一下:

private void animateViewToMatchParent(final View target) {
    int fromValue = target.getHeight();
    int toValue = ((View) target.getParent()).getHeight();

    ValueAnimator animator = ValueAnimator.ofInt(fromValue, toValue);

    animator.setDuration(250);
    animator.setInterpolator(new DecelerateInterpolator());
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            target.getLayoutParams().height = (int) animation.getAnimatedValue();
            target.requestLayout();
        }
    });
}