如何在Android Motion布局中为可绘制对象设置动画

问题描述

我正在尝试动作布局,我想在上面有一个带有动画图标的浮动动作按钮。我把所有的运动布局的东西都弄下来了,可以工作了。但是我找不到让动画FAB图标正常工作的方法。

// ic_menu_toggle_arrow_down is my Animated Vector Drawable    
<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/menuToggle"
        android:clickable="true"
        android:src="@drawable/ic_menu_toggle_arrow_down"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

基本上,我想在运动布局动画(来回)旁边对FAB图标进行动画处理。而且,我想在运动布局场景中最好

感谢任何有创意的人

解决方法

添加ConstraintLayout 依赖性

implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta1'

如果您不使用AndroidX,请添加以下支持库依赖项:

implementation 'com.android.support.constraint:constraint-layout:2.0.0-beta1'

这是一个完整的示例MotionLayout文件,可用于创建运动

    <?xml version="1.0" encoding="utf-8"?>
<!-- activity_main.xml -->
<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:id="@+id/motionLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutDescription="@xml/scene_01"
    tools:showPaths="true">

    <View
        android:id="@+id/button"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:background="@color/colorAccent"
        android:text="Button" />

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

创建MotionScene:在前面的MotionLayout示例中,app:layoutDescription属性引用了MotionScene。 MotionScene是XML资源文件,其中包含对应布局的所有运动描述。为了使布局信息与运动描述分开,每个MotionLayout都引用一个单独的MotionScene。请注意,MotionScene中的定义优先于MotionLayout中的任何类似定义。

这是一个描述基本水平运动的示例MotionScene文件

   <?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">

    <Transition
        motion:constraintSetStart="@+id/start"
        motion:constraintSetEnd="@+id/end"
        motion:duration="1000">
        <OnSwipe
            motion:touchAnchorId="@+id/button"
            motion:touchAnchorSide="right"
            motion:dragDirection="dragRight" />
    </Transition>

    <ConstraintSet android:id="@+id/start">
        <Constraint
            android:id="@+id/button"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_marginStart="8dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent" />
    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@+id/button"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_marginEnd="8dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintTop_toTopOf="parent" />
    </ConstraintSet>

</MotionScene>

有关更多信息,您可以参考以下地址: https://developer.android.com/training/constraint-layout/motionlayout#androidx

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...