2015-04-27 39 views
2

当我点击一个RelativeLayout时,它改变了大小。 我如何使这个改变动画?所以它看起来像矩形增长到全屏?动画改变LayoutParams(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); 
} 

回答

5

要在这种情况下,明确动画,你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"应该工作。

对于从人切特·哈泽更多的细节,请参见:https://www.youtube.com/watch?v=55wLsaWpQ4g

+1

我接着说:机器人:animateLayoutChanges = “true”'但对我的例子不起作用 – Laire

+0

已更新的答案,请尝试。 ;) – alenz316

+0

非常酷。谢谢。 – Laire

0

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

0

简单的技巧为我工作:

添加:

android:animateLayoutChanges="true" 

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

然后,加入这一行的在Java /科特林代码代码改变PARAMS前:

的Java:

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

科特林:

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