【问题标题】:Scale a layout to match parent with animation缩放布局以匹配父级与动画
【发布时间】:2015-08-16 16:59:47
【问题描述】:

在我的示例项目中,我有一个线性布局,它的高度设置为在 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>

【问题讨论】:

  • 如果您发布您的 xml,则更容易给出明确的建议。
  • @Christian 我已经编辑了我的问题

标签: android android-animation


【解决方案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 匹配父级

【讨论】:

    【解决方案2】:

    您可能需要尝试在父布局本身中添加 android:animateLayoutChanges="true",例如:

    <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

    【讨论】:

    • 我试过了,但没有用。我已经编辑了我的问题
    • android:animateLayoutChanges="true" 只会在 2 种情况下为父布局中的视图设置动画: 1. 添加或删除视图。 2. 设置视图的可见性。
    • 但是更改 layout_height 属性不会为视图设置动画。您可能需要以编程方式对其进行动画处理。
    【解决方案3】:

    我认为依赖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

    【讨论】:

      【解决方案4】:

      试试这个:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-18
        • 2014-11-27
        • 1970-01-01
        • 2019-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多