更改LayoutParams(RelativeLayout)的动画 [英] Animate changing LayoutParams (RelativeLayout)
本文介绍了更改LayoutParams(RelativeLayout)的动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我单击RelativeLayout时,它会更改大小. 我怎样才能使这一变化动起来?这样看起来矩形会变成全屏显示吗?
when I click on a RelativeLayout it change the size. How can I make this change animated? So it looks like the rectangle grows to full screen?
我的布局:
<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);
}
推荐答案
在这种情况下,要显式制作动画,您的sleepRoom()
应该看起来像这样.
To explicitly animate in this case, your sleepRoom()
should look like this.
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"
应该可以工作.
However, normally android:animateLayoutChanges="true"
should work.
有关切特·海瑟(Chet Haase)男子的更多详细信息,请参见: https://www.youtube. com/watch?v = 55wLsaWpQ4g
For more specifics from the man Chet Haase, see: https://www.youtube.com/watch?v=55wLsaWpQ4g
这篇关于更改LayoutParams(RelativeLayout)的动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文