在android中使用动画更改布局宽度
我试图用动画来改变我的相对动画宽度。我的目标是改变环绕内容宽度,使之与动画的父项相匹配。这是我的源代码在android中使用动画更改布局宽度,android,android-layout,android-animation,Android,Android Layout,Android Animation,我试图用动画来改变我的相对动画宽度。我的目标是改变环绕内容宽度,使之与动画的父项相匹配。这是我的源代码 private void setWidthAnimation(View view, int currentHeight, int newHeight) { ValueAnimator slideAnimator = ValueAnimator .ofInt(currentHeight, newHeight) .setDuration(10
private void setWidthAnimation(View view, int currentHeight, int newHeight) {
ValueAnimator slideAnimator = ValueAnimator
.ofInt(currentHeight, newHeight)
.setDuration(1000);
slideAnimator.addUpdateListener(animation -> {
Integer value = (Integer) animation.getAnimatedValue();
view.getLayoutParams().width = value.intValue();
view.requestLayout();
});
AnimatorSet set = new AnimatorSet();
set.play(slideAnimator);
set.setInterpolator(new BounceInterpolator());
set.start();
view.setBackgroundColor(Color.RED);
}
我这样称呼这个方法
if (isKeyBoardOpen)
setWidthAnimation(gifEditableLayout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
else
setWidthAnimation(gifEditableLayout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
但当我运行我的应用程序时,视图的宽度已经改变,但没有动画。谁能解释一下我做错了什么?
谢谢尝试将android:animateLayoutChanges=“true”放在父视图(XML)上。 更多信息:
我真的不知道你的代码是怎么回事,但我可以向你建议我的选择:将你的相对对象放入约束中,并使用约束集动画。 启动xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootContainer"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/rLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
代码的主要问题是,实际上没有宽度的实际值 如果查看
setWidthAnimation()
方法,请查看其中的内容:
ValueAnimator slideAnimator = ValueAnimator
.ofInt(currentHeight, newHeight)
.setDuration(1000);
特别是查看currentHeight和newHeight。你知道这两个int变量的值是多少吗
setWidthAnimation(gifEditableLayout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
您正在使用ViewGroup.LayoutParams.WRAP_CONTENT
和ViewGroup.LayoutParams.MATCH_PARENT
作为宽度值。但是,这两个都不是视图的实际宽度
相反,它们只是LayoutParams
的静态常量:
是一个静态int常量,值为-1MATCH\u PARENT
是一个静态int常量,值为-2WRAP\u CONTENT
- layoutParams.width=-1这与调用
或FILL\u PARENT
MATCH\u PARENT
- layoutParams.width=-2这与调用
WRAP\u CONTENT
- layoutParams.width=任何其他整数这与直接将宽度设置为此值相同
WRAP\u CONTENT
。下一个可能的int值是-2,这是完成的值,因此它将在1秒后完全更新。一秒钟后,它更新并运行您的代码,该代码将视图的宽度设置为-2。这会立即将视图强制为MATCH\u PARENT
因此,要解决这个问题,需要将实际整数值传递给
setWidthAnimation()
方法 谢谢你的回复。这很有帮助
setWidthAnimation(gifEditableLayout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);