Android 设置LayoutLayout中LayoutParams更改的动画
我正在创建一个应用程序,其中有一个带有搜索栏的屏幕。 我的活动布局如下所示:Android 设置LayoutLayout中LayoutParams更改的动画,android,animation,layoutparams,android-constraintlayout,Android,Animation,Layoutparams,Android Constraintlayout,我正在创建一个应用程序,其中有一个带有搜索栏的屏幕。 我的活动布局如下所示: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="ver
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:id="@+id/wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true">
<TextView
android:id="@+id/title"
android:textColor="@color/colorPrimary"
android:text="@string/mainScreenTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAlignment="center"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:paddingTop="100dp"/>
<com.arlib.floatingsearchview.FloatingSearchView
android:id="@+id/searchView"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginTop="100dp"
android:animateLayoutChanges="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title"/>
</android.support.constraint.ConstraintLayout>
</RelativeLayout>
app:layout_constraintTop_toTopOf="parent"
android:layout_height="500dp"
稍后,我将不得不将其向后移动,并再次将高度更改为默认值
app:layout_constraintTop_toBottomOf="@+id/title"
android:layout_height="50dp"
问题是,我必须给它设置动画。我花了很多时间寻找解决方案,但不幸的是,我什么也没找到。尝试使用
动画
类:
Animation animation = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
LayoutParams params = yourView.getLayoutParams();
// modify your layout params here
yourView.setLayoutParams(params);
}
};
animation.setDuration(500); // in ms
yourView.startAnimation(animation);
@不幸的是,比伯的版本也不适合我。尽管这个问题已经很老了,但这里有一个对我有用的解决方案,使用TransitionManager。这有点“冗余”,因为您必须复制一些代码,但因此非常简单,只包括两个基本步骤:
在应用程序启动时包含您不想使用的版本,以前您使用过约束布局。这样,就不必复制整个布局文件。然而,当我的include元素有一个id时,这导致findViewById()方法返回null浮动\u搜索\u查看\u底部.xml
:
<com.arlib.floatingsearchview.FloatingSearchView
android:id="@+id/searchView"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginTop="100dp"
android:animateLayoutChanges="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title"
android:layout_height="50dp" />
boolean toggle=false//你应该给这家伙一个更具描述性的名字!
上下文=此;//在活动范围内。
公共无效动画转换(){
//从两个布局加载约束集
ConstraintSet atBottom=新ConstraintSet();
ConstraintSet ATOP=新ConstraintSet();
克隆(上下文、右布局、浮动搜索、底部视图);
atTop.clone(上下文、右布局、浮动搜索、顶部视图);
//根据“切换”应用约束集
ConstraintSet newLayout=(切换)?展开:折叠;
TransitionManager.beginDelayedTransition(constraintLayout);
newLayout.applyTo(constraintLayout);
//反转开关
切换=!切换;
}
注意:
1.确保context
属于活动类型,并且您正在使用
AppCompat主题。我不小心使用了getApplicationContext()
,什么
很明显导致了车祸。
2.如果您在您的TK上收到NPE
我在applyTransformation方法中执行params.toptotom=-1和params.topToTop=R.id.wrapper,但它仍然没有动画:cstill没有动画