短视频系统,Android 使用MotionLayout实现动画
短视频系统,Android 使用MotionLayout实现动画效果实现的相关代码
MotionLayout最简单的使用
首先,我们新建动画开始时候的布局activity_motion_layout3_start.xml
1 | <br><androidx.constraintlayout.widget.ConstraintLayout xmlns:android= "http://schemas.android.com/apk/res/android" <br> xmlns:app= "http://schemas.android.com/apk/res-auto" <br> android:layout_width= "match_parent" <br> android:layout_height= "match_parent" ><br> <View<br> android:id= "@+id/button" <br> android:layout_width= "64dp" <br> android:layout_height= "64dp" <br> android:background= "@color/colorAccent" <br> android:text= "Button" <br> android:layout_marginLeft= "16dp" <br> app:layout_constraintBottom_toBottomOf= "parent" <br> app:layout_constraintLeft_toLeftOf= "parent" <br> app:layout_constraintTop_toTopOf= "parent" /><br></androidx.constraintlayout.widget.ConstraintLayout> |
新建一个动画结束时候的布局activity_motion_layout3_end.xml
1 | <androidx.constraintlayout.widget.ConstraintLayout xmlns:android= "http://schemas.android.com/apk/res/android" <br> xmlns:app= "http://schemas.android.com/apk/res-auto" <br> android:layout_width= "match_parent" <br> android:layout_height= "match_parent" ><br> <View<br> android:id= "@+id/button" <br> android:layout_width= "64dp" <br> android:layout_height= "64dp" <br> android:layout_marginRight= "16dp" <br> android:background= "@color/colorAccent" <br> android:text= "Button" <br> app:layout_constraintBottom_toBottomOf= "parent" <br> app:layout_constraintRight_toRightOf= "parent" <br> app:layout_constraintTop_toTopOf= "parent" /><br></androidx.constraintlayout.widget.ConstraintLayout> |
然后,在Activity的xml中,使用MotionLayout,注意,这几个布局中的id需要相同
1 | <androidx.constraintlayout.motion.widget.MotionLayout<br> xmlns:android= "http://schemas.android.com/apk/res/android" <br> xmlns:tools= "http://schemas.android.com/tools" <br> xmlns:app= "http://schemas.android.com/apk/res-auto" <br> android:id= "@+id/motionLayout" <br> app:layoutDescription= "@xml/activity_main_scene3" <br> android:layout_width= "match_parent" <br> android:layout_height= "match_parent" ><br> <View<br> android:id= "@+id/button" <br> android:background= "@color/colorAccent" <br> android:layout_width= "64dp" <br> android:layout_height= "64dp" <br> android:text= "Button" <br> tools:layout_editor_absoluteX= "147dp" <br> tools:layout_editor_absoluteY= "230dp" /><br></androidx.constraintlayout.motion.widget.MotionLayout> |
这里的重点app:layoutDescription,我们设置了activity_main_scene3.xml,这里文件就是具体设置动画的地方了
1 | <br><MotionScene xmlns:android= "http://schemas.android.com/apk/res/android" <br> xmlns:app= "http://schemas.android.com/apk/res-auto" ><br> <Transition<br> app:constraintSetEnd= "@layout/activity_motion_layout3_end" <br> app:constraintSetStart= "@layout/activity_motion_layout3_start" ><br> <OnSwipe<br> app:dragDirection= "dragRight" <br> app:touchAnchorId= "@id/button" <br> app:touchAnchorSide= "right" /><br> </Transition><br></MotionScene> |
以上就是 短视频系统,Android 使用MotionLayout实现动画效果实现的相关代码,更多内容欢迎关注之后的文章
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现