具有ConstraintLayout的ValueAnimator

时间:2018-04-23 08:40:39

标签: android animation kotlin

我对扩展/折叠动画的实现有问题。我使用Constraint布局和ValueAnimator。当我尝试扩展我的视图时会出现问题。当我将可见性设置为VISIBLE时,它会显示其全高,然后按照我的预期制作动画。这是一个显示问题的gif:

enter image description here

这是我的布局实现:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinationMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/back_inbox">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/toolbat_height"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <include
            android:id="@+id/header"
            layout="@layout/header_element" />

    </android.support.v7.widget.Toolbar>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/_5sdp"
        android:layout_marginEnd="@dimen/_5sdp"
        android:layout_marginLeft="@dimen/_5sdp"
        android:layout_marginRight="@dimen/_5sdp"
        android:layout_marginStart="@dimen/_5sdp"
        android:layout_weight="1"
        android:background="@drawable/border_white"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="@dimen/_2sdp"
        app:layout_constraintBottom_toTopOf="@id/listWrapper"
        app:layout_constraintRight_toLeftOf="@id/cancel"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar">

        <ImageView
            android:layout_width="@dimen/_20sdp"
            android:layout_height="@dimen/_20sdp"
            android:padding="@dimen/_3sdp"
            android:src="@drawable/icon_search" />

        <com.pleez.customTextview.EdittextDisplayRegular
            android:id="@+id/searchFiled"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:hint="@string/search"
            android:maxLines="1"
            android:paddingBottom="@dimen/_2sdp"
            android:paddingLeft="@dimen/_10sdp"
            android:paddingRight="@dimen/_10sdp"
            android:paddingTop="@dimen/_2sdp"
            android:singleLine="true"
            android:textColor="#929196"
            android:textSize="@dimen/_11sdp"/>

    </LinearLayout>

    <com.pleez.customTextview.TextViewDisplayBold
        android:id="@+id/cancel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:maxLines="1"
        android:layout_marginEnd="@dimen/_5sdp"
        android:layout_marginRight="@dimen/_5sdp"
        android:layout_marginBottom="@dimen/_3sdp"
        android:singleLine="true"
        android:textColor="@color/archive"
        android:textSize="@dimen/_11sdp"
        android:text="@string/cancel"
        android:visibility="gone"
        app:layout_constraintBottom_toTopOf="@id/listWrapper"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        app:layout_constraintRight_toRightOf="parent"/>

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/listWrapper"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/linearLayout">

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipeRefreshLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">


                <FrameLayout
                    android:id="@+id/empty"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:visibility="gone" />

                <com.pleez.helper.TouchyRecyclerView
                    android:id="@+id/recyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@color/white"
                    android:visibility="visible" />

            </LinearLayout>

        </android.support.v4.widget.SwipeRefreshLayout>

        <RelativeLayout
            android:id="@+id/searchView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <View
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:alpha="0.1"
                android:background="@color/black" />

            <com.pleez.helper.TouchyRecyclerView
                android:id="@+id/searchRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/transparent"
                android:visibility="visible" />

        </RelativeLayout>
    </android.support.design.widget.CoordinatorLayout>
</android.support.constraint.ConstraintLayout>

折叠动画:

override fun collpse() {
    val toolbarHeight = resources.getDimension(R.dimen.toolbat_height)
    ValueAnimator.ofInt(0, 100).apply {
        duration = 500
        addUpdateListener {
            val toolbarParams = toolbar.layoutParams
            toolbarParams.height = (toolbarHeight - (toolbarHeight * animatedValue as Int / 100)).toInt()
            toolbar.layoutParams = toolbarParams
            toolbar.requestLayout()
        }
        addListener(object : Animator.AnimatorListener {
            override fun onAnimationRepeat(p0: Animator?) {
            }

            override fun onAnimationEnd(p0: Animator?) {
                searchView?.visibility = View.VISIBLE
                toolbar.visibility = View.GONE
            }

            override fun onAnimationCancel(p0: Animator?) {
            }

            override fun onAnimationStart(p0: Animator?) {
            }

        })
        start()
    }
}

展开动画:

override fun expand() {
    val toolbarHeight = resources.getDimension(R.dimen.toolbat_height)
    ValueAnimator.ofInt(0, 100).apply {
        duration = 500
        addUpdateListener {
            val toolbarParams = toolbar.layoutParams
            toolbarParams.height = (toolbarHeight * animatedValue as Int / 100).toInt()
            toolbar.requestLayout()
        }
        addListener(object : Animator.AnimatorListener {
            override fun onAnimationRepeat(p0: Animator?) {
            }

            override fun onAnimationEnd(p0: Animator?) {
                searchView?.visibility = View.GONE
            }

            override fun onAnimationCancel(p0: Animator?) {
            }

            override fun onAnimationStart(p0: Animator?) {
                val toolbarParams = toolbar.layoutParams
                toolbarParams.height = 0
                toolbar.layoutParams = toolbarParams
                toolbar.requestLayout()
                toolbar.visibility = View.VISIBLE
            }

        })
        start()
    }
}

谁能告诉我这里做错了什么?我甚至试图在动画开始时将视图高度设置为0,但它没有效果。

编辑

我还测试了从Animator.AnimatorListeners onAnimationStart移动设置高度的建议,结果与之前相同:

override fun expand() {
    val toolbarParams = toolbar.layoutParams
    toolbarParams.height = 0
    toolbar.layoutParams = toolbarParams
    toolbar.requestLayout()
    val toolbarHeight = resources.getDimension(R.dimen.toolbat_height)
    ValueAnimator.ofInt(0, 100).apply {
        duration = 500
        addUpdateListener {
            val toolbarParams = toolbar.layoutParams
            toolbarParams.height = (toolbarHeight * animatedValue as Int / 100).toInt()
            toolbar.requestLayout()
        }
        addListener(object : Animator.AnimatorListener {
            override fun onAnimationRepeat(p0: Animator?) {
            }

            override fun onAnimationEnd(p0: Animator?) {
                searchView?.visibility = View.GONE
            }

            override fun onAnimationCancel(p0: Animator?) {
            }

            override fun onAnimationStart(p0: Animator?) {
                toolbar.visibility = View.VISIBLE
            }

        })
        start()
    }
}

还有其他想法吗?

0 个答案:

没有答案