Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中使用动画更改布局宽度_Android_Android Layout_Android Animation - Fatal编程技术网

在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);
特别是查看currentHeightnewHeight。你知道这两个int变量的值是多少吗

setWidthAnimation(gifEditableLayout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
您正在使用
ViewGroup.LayoutParams.WRAP_CONTENT
ViewGroup.LayoutParams.MATCH_PARENT
作为宽度值。但是,这两个都不是视图的实际宽度

相反,它们只是
LayoutParams
的静态常量:

  • MATCH\u PARENT
    是一个静态int常量,值为-1

  • WRAP\u CONTENT
    是一个静态int常量,值为-2

因此,您实际上是告诉ValueAnimator在1秒的时间内从-1到-2或-2到-1的值设置动画

此外,您只需将-1或-2设置为LayoutParam的宽度。幸运的是,这确实实现了视图的完成的外观,因为视图的宽度有3种可能:

  • layoutParams.width=-1这与调用
    FILL\u PARENT
    MATCH\u PARENT

  • layoutParams.width=-2这与调用
    WRAP\u CONTENT

  • layoutParams.width=任何其他整数这与直接将宽度设置为此值相同

但是,不会发生动画

原因很简单,因为ValueAnimator实际上不做任何动画。你可以把它想象成一个数字计数器。您已经将其编码为在1秒的时间跨度内从-1到-2计算int值。因此,当它开始时,它位于
WRAP\u CONTENT
。下一个可能的int值是-2,这是完成的值,因此它将在1秒后完全更新。一秒钟后,它更新并运行您的代码,该代码将视图的宽度设置为-2。这会立即将视图强制为
MATCH\u PARENT


因此,要解决这个问题,需要将实际整数值传递给
setWidthAnimation()
方法

谢谢你的回复。这很有帮助
setWidthAnimation(gifEditableLayout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);