按钮现在可以在 android 工作室中使用动态布局

Buttons now working with motion layout in android studio

我在 android 工作室中使用动态布局制作了一个类似 YouTube 的过渡,但该布局内的所有按钮都不起作用,这可能是由于在 activity_main_scene.xml

中应用了滑动方法

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 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"
    app:layoutDescription="@xml/activity_main_scene"
    android:id="@+id/parent_layout"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toTopOf="@+id/constraintLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.337"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:listitem="@layout/audio_list_layout">

    </androidx.recyclerview.widget.RecyclerView>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="0dp"
        android:layout_height="70dp"
        android:background="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="0dp"
        android:layout_height="4dp"
        android:layout_centerInParent="true"
        android:orientation="horizontal"
        android:progress="90"
        app:layout_constraintEnd_toEndOf="@id/constraintLayout"
        app:layout_constraintStart_toStartOf="@id/constraintLayout"
        app:layout_constraintTop_toTopOf="@id/constraintLayout" />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        app:layout_constraintEnd_toEndOf="@id/constraintLayout"
        app:layout_constraintStart_toStartOf="@id/constraintLayout"
        app:layout_constraintTop_toTopOf="@id/constraintLayout" />

    <TextView
        android:id="@+id/titleView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:maxWidth="220dp"
        android:scrollHorizontally="true"
        android:singleLine="true"
        app:layout_constraintBottom_toBottomOf="@id/constraintLayout"
        app:layout_constraintStart_toStartOf="@id/constraintLayout"
        app:layout_constraintTop_toTopOf="@id/constraintLayout" />

    <TextView
        android:id="@+id/startTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:visibility="invisible"
        android:layout_marginStart="16dp"
        android:text="@string/_00_00"
        app:layout_constraintBottom_toBottomOf="@id/seekBar"
        app:layout_constraintStart_toStartOf="@id/constraintLayout" />

    <TextView
        android:id="@+id/EndTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:visibility="invisible"
        android:layout_marginEnd="16dp"
        android:text="@string/_00_00"
        app:layout_constraintBottom_toBottomOf="@id/seekBar"
        app:layout_constraintEnd_toEndOf="@id/constraintLayout" />


    <ImageButton
        android:id="@+id/close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="?attr/selectableItemBackground"
        android:contentDescription="@string/close"
        android:padding="10dp"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="@id/constraintLayout"
        app:layout_constraintEnd_toEndOf="@id/constraintLayout"
        app:layout_constraintTop_toTopOf="@id/constraintLayout"
        app:srcCompat="@drawable/ic_white_baseline_keyboard_arrow_down_32" />

    <ImageButton
        android:id="@+id/playPause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="?attr/selectableItemBackground"
        android:contentDescription="@string/playPause"
        android:padding="10dp"
        app:layout_constraintBottom_toBottomOf="@id/constraintLayout"
        app:layout_constraintEnd_toStartOf="@+id/next"
        app:layout_constraintTop_toTopOf="@id/constraintLayout"
        app:srcCompat="@drawable/ic_white_baseline_play_arrow_32" />

    <ImageButton
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="?attr/selectableItemBackground"
        android:contentDescription="@string/next"
        android:padding="10dp"
        app:layout_constraintBottom_toBottomOf="@id/constraintLayout"
        app:layout_constraintEnd_toEndOf="@+id/constraintLayout"
        app:layout_constraintTop_toTopOf="@id/constraintLayout"
        app:srcCompat="@drawable/ic_white_baseline_skip_next_32" />

    <ImageButton
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="?attr/selectableItemBackground"
        android:contentDescription="@string/previous"
        android:padding="10dp"
        app:layout_constraintBottom_toBottomOf="@id/constraintLayout"
        app:layout_constraintEnd_toStartOf="@id/playPause"
        app:layout_constraintTop_toTopOf="@id/constraintLayout"
        app:srcCompat="@drawable/ic_white_baseline_skip_previous_32" />


</androidx.constraintlayout.motion.widget.MotionLayout>

activity_main_scene.xml

<?xml version="1.0" encoding="utf-8"?>
<MotionScene 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <Transition
        motion:constraintSetStart="@id/start"
        motion:constraintSetEnd="@id/end"
        motion:duration="400"
        motion:motionInterpolator="linear">

        <OnSwipe
            motion:maxAcceleration="800"
            motion:dragDirection="dragUp"
            motion:touchRegionId="@id/constraintLayout"
            motion:touchAnchorId="@+id/constraintLayout"
            motion:touchAnchorSide="top" />

       <KeyFrameSet>
       </KeyFrameSet>
        <OnClick motion:targetId="@+id/close" />
    </Transition>

    <ConstraintSet android:id="@+id/start">
        <Constraint
            android:id="@+id/my_recycler_view"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:scrollbars="vertical"
            motion:layout_constraintBottom_toTopOf="@+id/constraintLayout"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintHorizontal_bias="0.337"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent"
            tools:listitem="@layout/audio_list_layout"
            />

        <Constraint
            android:id="@+id/constraintLayout"
            android:layout_width="0dp"
            android:layout_height="70dp"
            android:background="@color/colorPrimary"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            />

        <Constraint
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="0dp"
            android:layout_height="4dp"
            android:layout_centerInParent="true"
            android:orientation="horizontal"
            android:progress="90"
            motion:layout_constraintEnd_toEndOf="@id/constraintLayout"
            motion:layout_constraintStart_toStartOf="@id/constraintLayout"
            motion:layout_constraintTop_toTopOf="@id/constraintLayout"
            />

        <Constraint
            android:id="@+id/seekBar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:visibility="invisible"
            motion:layout_constraintEnd_toEndOf="@id/constraintLayout"
            motion:layout_constraintStart_toStartOf="@id/constraintLayout"
            motion:layout_constraintTop_toTopOf="@id/constraintLayout"
            />

        <Constraint
            android:id="@+id/startTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:visibility="invisible"
            android:layout_marginStart="16dp"
            android:text="@string/_00_00"
            motion:layout_constraintBottom_toBottomOf="@id/seekBar"
            motion:layout_constraintStart_toStartOf="@id/constraintLayout" />

        <Constraint
            android:id="@+id/EndTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:visibility="invisible"
            android:layout_marginEnd="16dp"
            android:text="@string/_00_00"
            motion:layout_constraintBottom_toBottomOf="@id/seekBar"
            motion:layout_constraintEnd_toEndOf="@id/constraintLayout" />

        <Constraint
            android:id="@+id/titleView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:marqueeRepeatLimit="marquee_forever"
            android:maxWidth="220dp"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:textColor="#000000"
            motion:layout_constraintBottom_toBottomOf="@id/constraintLayout"
            motion:layout_constraintStart_toStartOf="@id/constraintLayout"
            motion:layout_constraintTop_toTopOf="@id/constraintLayout"
            />

        <Constraint
            android:id="@+id/close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?attr/selectableItemBackground"
            android:contentDescription="@string/close"
            android:visibility="invisible"
            android:padding="10dp"
            motion:layout_constraintBottom_toBottomOf="@id/constraintLayout"
            motion:layout_constraintEnd_toEndOf="@id/constraintLayout"
            motion:layout_constraintTop_toTopOf="@id/constraintLayout"
            motion:srcCompat="@drawable/ic_white_baseline_keyboard_arrow_down_32"
            />

        <Constraint
            android:id="@+id/playPause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?attr/selectableItemBackground"
            android:contentDescription="@string/playPause"
            android:padding="10dp"
            motion:layout_constraintBottom_toBottomOf="@id/constraintLayout"
            motion:layout_constraintEnd_toStartOf="@+id/next"
            motion:layout_constraintTop_toTopOf="@id/constraintLayout"
            motion:srcCompat="@drawable/ic_white_baseline_play_arrow_32"
            />

        <Constraint
            android:id="@+id/next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?attr/selectableItemBackground"
            android:contentDescription="@string/next"
            android:padding="10dp"
            motion:layout_constraintBottom_toBottomOf="@id/constraintLayout"
            motion:layout_constraintEnd_toEndOf="@+id/constraintLayout"
            motion:layout_constraintTop_toTopOf="@id/constraintLayout"
            motion:srcCompat="@drawable/ic_white_baseline_skip_next_32"
            />

        <Constraint
            android:id="@+id/start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?attr/selectableItemBackground"
            android:contentDescription="@string/previous"
            android:padding="10dp"
            motion:layout_constraintBottom_toBottomOf="@id/constraintLayout"
            motion:layout_constraintEnd_toStartOf="@id/playPause"
            motion:layout_constraintTop_toTopOf="@id/constraintLayout"
            motion:srcCompat="@drawable/ic_white_baseline_skip_previous_32"
            />

        

    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        
        <Constraint
            android:id="@+id/my_recycler_view"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:scrollbars="vertical"
            motion:layout_constraintBottom_toTopOf="@+id/constraintLayout"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent"
            tools:listitem="@layout/audio_list_layout"
            />

        <Constraint
            android:id="@+id/constraintLayout"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@color/colorPrimary"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent"
            />

        <Constraint
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="0dp"
            android:layout_height="4dp"
            android:layout_centerInParent="true"
            android:orientation="horizontal"
            android:progress="90"
            android:visibility="invisible"
            motion:layout_constraintEnd_toEndOf="@id/constraintLayout"
            motion:layout_constraintTop_toTopOf="@id/constraintLayout"
            />

        <Constraint
            android:id="@+id/seekBar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="32dp"
            motion:layout_constraintBottom_toTopOf="@+id/playPause"
            motion:layout_constraintEnd_toEndOf="@id/constraintLayout"
            motion:layout_constraintStart_toStartOf="@id/constraintLayout"
            />

        <Constraint
            android:id="@+id/startTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="32dp"
            android:layout_marginStart="16dp"
            android:text="@string/_00_00"
            motion:layout_constraintBottom_toBottomOf="@id/seekBar"
            motion:layout_constraintStart_toStartOf="@id/constraintLayout" />

        <Constraint
            android:id="@+id/EndTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="32dp"
            android:layout_marginEnd="16dp"
            android:text="@string/_00_00"
            motion:layout_constraintBottom_toBottomOf="@id/seekBar"
            motion:layout_constraintEnd_toEndOf="@id/constraintLayout" />

        <Constraint
            android:id="@+id/titleView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="72dp"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:marqueeRepeatLimit="marquee_forever"
            android:maxWidth="220dp"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:textColor="#000000"
            motion:layout_constraintBottom_toTopOf="@+id/seekBar"
            motion:layout_constraintEnd_toEndOf="@id/constraintLayout"
            motion:layout_constraintStart_toStartOf="@id/constraintLayout"
            />

        <Constraint
            android:id="@+id/close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?attr/selectableItemBackground"
            android:contentDescription="@string/close"
            android:padding="10dp"
            motion:layout_constraintStart_toStartOf="@id/constraintLayout"
            motion:layout_constraintTop_toTopOf="@id/constraintLayout"
            motion:srcCompat="@drawable/ic_white_baseline_keyboard_arrow_down_32"
            />

        <Constraint
            android:id="@+id/playPause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="64dp"
            android:background="?attr/selectableItemBackground"
            android:contentDescription="@string/playPause"
            android:padding="10dp"
            motion:layout_constraintBottom_toBottomOf="@id/constraintLayout"
            motion:layout_constraintEnd_toStartOf="@+id/next"
            motion:layout_constraintStart_toEndOf="@+id/start"
            motion:srcCompat="@drawable/ic_white_baseline_play_arrow_32"
            />

        <Constraint
            android:id="@+id/next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="64dp"
            android:background="?attr/selectableItemBackground"
            android:contentDescription="@string/next"
            android:padding="10dp"
            motion:layout_constraintBottom_toBottomOf="@id/constraintLayout"
            motion:layout_constraintEnd_toEndOf="@id/constraintLayout"
            motion:layout_constraintStart_toEndOf="@+id/playPause"
            motion:srcCompat="@drawable/ic_white_baseline_skip_next_32"
            />

        <Constraint
            android:id="@+id/start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="64dp"
            android:background="?attr/selectableItemBackground"
            android:contentDescription="@string/previous"
            android:padding="10dp"
            motion:layout_constraintBottom_toBottomOf="@id/constraintLayout"
            motion:layout_constraintEnd_toStartOf="@+id/playPause"
            motion:layout_constraintHorizontal_bias="0.49"
            motion:layout_constraintStart_toStartOf="@id/constraintLayout"
            motion:srcCompat="@drawable/ic_white_baseline_skip_previous_32"
            />


    </ConstraintSet>

</MotionScene>

开始和结束的渲染场景如下图

[1

下面的黑条是有变化的,所有按钮现在都可以使用,因为它只有运动布局有效

即使是按钮,搜索栏也不会在点击时显示任何动画,只是因为运动布局而被禁用

activity_main_scene.xml

(第 16 行)

我变了

motion:touchRegionId="@id/constraintLayout"

motion:touchRegionId="@id/parent_layout"

现在可以正常工作了

建议您使用此覆盖 class 进行动态布局。

package com.uoooo.simple.example.ui.common.widget

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.FrameLayout
import androidx.constraintlayout.motion.widget.MotionLayout
import com.uoooo.simple.example.R

// https://medium.com/vrt-digital-studio/picture-in-picture-video-overlay-with-motionlayout-a9404663b9e7
class VideoOverlayView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
    private val motionLayout: MotionLayout =
        LayoutInflater.from(context).inflate(R.layout.layout_detail, this, false) as MotionLayout
    private val touchableArea: View

    private val clickableArea: View

    private var startX: Float? = null
    private var startY: Float? = null

    init {
        touchableArea = motionLayout.findViewById(R.id.motionInteractView)
        clickableArea = motionLayout.findViewById(R.id.playerView)
        addView(motionLayout)
    }

    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
        val isInProgress = (motionLayout.progress > 0.0f && motionLayout.progress < 1.0f)
        val isInTarget = touchEventInsideTargetViewExceptTop(touchableArea, ev)

        return if (isInProgress || isInTarget) {
            super.onInterceptTouchEvent(ev)
        } else {
            true
        }
    }

    private fun touchEventInsideTargetView(v: View, ev: MotionEvent): Boolean {
        if (ev.x > v.left && ev.x < v.right) {
            if (ev.y > v.top && ev.y < v.bottom) {
                return true
            }
        }
        return false
    }

    private fun touchEventInsideTargetViewExceptTop(v: View, ev: MotionEvent): Boolean {
        if (ev.x > v.left && ev.x < v.right) {
            if (ev.y > v.top) {
                return true
            }
        }
        return false
    }

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        val consumed = clickableArea.dispatchTouchEvent(ev)
        if (consumed) {
            return consumed
        }
        if (touchEventInsideTargetView(clickableArea, ev)) {
            when (ev.action) {
                MotionEvent.ACTION_DOWN -> {
                    startX = ev.x
                    startY = ev.y
                }

                MotionEvent.ACTION_UP -> {
                    if (startX != null && startY != null) {
                        val endX = ev.x
                        val endY = ev.y
                        if (isClick(startX!!, endX, startY!!, endY)) {
                            if (motionLayout.currentState == motionLayout.startState) {
                                clickableArea.performClick()
                            }
                            if (doClickTransition()) {
                                return true
                            }
                        }
                    }
                }
            }
        }

        return super.dispatchTouchEvent(ev)
    }

    private fun doClickTransition(): Boolean {
        var isClickHandled = false
        if (motionLayout.progress < 0.05F) {
//            motionLayout.transitionToEnd()
//            isClickHandled = true
        } else if (motionLayout.progress > 0.95F) {
            motionLayout.transitionToStart()
            isClickHandled = true
        }
        return isClickHandled
    }

    private fun isClick(startX: Float, endX: Float, startY: Float, endY: Float): Boolean {
        val differenceX = Math.abs(startX - endX)
        val differenceY = Math.abs(startY - endY)
        return !/* =5 */(differenceX > 200 || differenceY > 200)
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent?): Boolean {
        return false
    }
}