【问题标题】:Animate changing LayoutParams (RelativeLayout)动画改变 LayoutParams (RelativeLayout)
【发布时间】:2015-04-27 15:45:26
【问题描述】:

当我单击 RelativeLayout 时,它会更改大小。 我怎样才能使这个变化动画化?所以看起来矩形长到了全屏?

我的布局:

<RelativeLayout 
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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context=".MainActivity"
android:background="#ff000000">

<RelativeLayout
    android:id="@+id/srLayout"
    android:layout_width="62.5dp"
    android:layout_height="132.5dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="false"
    android:layout_alignParentRight="true"
    android:layout_marginRight="10dp"
    android:layout_marginEnd="10dp"
    android:layout_marginTop="10dp"
    android:clickable="true"
    android:onClick="sleepRoom"
    android:background="#ffffffff"></RelativeLayout>

</RelativeLayout>

代码:

public void sleepRoom(View view) {
    RelativeLayout sr = (RelativeLayout) findViewById(view.getId());
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) sr.getLayoutParams();
    params.height = RelativeLayout.LayoutParams.MATCH_PARENT;
    params.width = RelativeLayout.LayoutParams.MATCH_PARENT;
    sr.setLayoutParams(params);
}

【问题讨论】:

    标签: android android-layout android-animation


    【解决方案1】:

    对我有用的简单技巧:

    添加:

    android:animateLayoutChanges="true"
    

    到您正在更改其 layoutParams 的视图的 xml。

    然后,在您的 Java/Kotlin 代码中添加这行代码,然后再更改参数:

    Java:

    ((ViewGroup) sr).getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
    

    科特林:

    (sr as ViewGroup).layoutTransition.enableTransitionType(LayoutTransition.CHANGING)
    

    【讨论】:

      【解决方案2】:

      要在这种情况下显式设置动画,您的 sleepRoom() 应该如下所示。

      public void sleepRoom(View view) {
          final RelativeLayout sr = (RelativeLayout) view;
          RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) sr.getLayoutParams();
          params.height = RelativeLayout.LayoutParams.MATCH_PARENT;
          params.width = RelativeLayout.LayoutParams.MATCH_PARENT;
          sr.setLayoutParams(params);
      
          PropertyValuesHolder pvhLeft = PropertyValuesHolder.ofInt("left", 0, 1);
          PropertyValuesHolder pvhTop = PropertyValuesHolder.ofInt("top", 0, 1);
          PropertyValuesHolder pvhRight = PropertyValuesHolder.ofInt("right", 0, 1);
          PropertyValuesHolder pvhBottom = PropertyValuesHolder.ofInt("bottom", 0, 1);
          PropertyValuesHolder pvhRoundness = PropertyValuesHolder.ofFloat("roundness", 0, 1);
      
      
          final Animator collapseExpandAnim = ObjectAnimator.ofPropertyValuesHolder(sr, pvhLeft, pvhTop,
                  pvhRight, pvhBottom, pvhRoundness);
          collapseExpandAnim.setupStartValues();
      
          sr.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
              @Override
              public boolean onPreDraw() {
                  sr.getViewTreeObserver().removeOnPreDrawListener(this);
                  collapseExpandAnim.setupEndValues();
                  collapseExpandAnim.start();
                  return false;
              }
          });
      }
      

      但是,通常android:animateLayoutChanges="true" 应该可以工作。

      有关 Chet Haase 的更多细节,请参阅:https://www.youtube.com/watch?v=55wLsaWpQ4g

      【讨论】:

      • 我添加了android:animateLayoutChanges="true",但不适用于我的示例
      • 更新答案,试试看。 ;)
      • 这只是为了扩展吗?如何崩溃?谢谢。
      • 不,你需要动画然后改变你的 LayoutParams;因为你不能在棒棒糖之前画出你的界限。因为如果在制作动画之前缩小布局,动画就会被剪裁。
      【解决方案3】:

      因为RelativeLayout 扩展了View,您可以在“常规视图” 上使用您正在使用的所有动画。例如尝试使用animate.scale() 看看它是否适合您。一个简单的方法是这样的

      @Override
      protected void onCreate (Bundle savedInstanceState) {
      
         sr = (RelativeLayout) findViewById(view.getId());
         sv.setVisibility(View.GONE);
      
         scaleEnd = sr.getScaleX();
         sr.setScaleX(0);
      }
      
      ...
      @Override
      protected void onPostCreate (Bundle savedInstanceState) {
          super.onPostCreate(savedInstanceState);
          sr.setVisibility(View.Visible);
          sr.animate.scaleX(scaleEnd);
      }
      

      另外,如果你想要 Lollipop 类型的动画,请查看:Use the Reveal Effect

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-30
        • 2020-11-24
        • 2019-01-15
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        • 2017-10-06
        相关资源
        最近更新 更多