微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何在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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。