当前位置:网站首页>ConstraintSet of animation of ContrstrainLayout
ConstraintSet of animation of ContrstrainLayout
2022-08-04 07:32:00 【Mr_Tony】
一、前言
ConstraintLayout
Has some of its own animation effects,比如:MotionLayout
、ConstraintSet
.One of them is recorded hereConstraintSet
的使用方式.In fact, this animation method can be usedMotionLayout
替换掉的,and it will be simpler
二、使用方式
1、使用范围
使用ConstraintSet
Animate the keyframes of the layout to animate the movement,But the year of the animation changes position and size,无法改变颜色
2、使用xml方式
This way requires two servingsxml文件,Keep the controls that need to be moved the same in both layoutsid.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/constraint_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
<TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="标题" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/>
<Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击暴富" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
frame_two.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/constraint_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
<TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="标题" android:layout_marginTop="100dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
class MainActivity : AppCompatActivity() {
var constraintLayout: ConstraintLayout ?= null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
constraintLayout = findViewById(R.id.constraint_layout)
lifecycleScope.launch {
delay(1000)
animateToKeyframeTwo()
}
}
private fun animateToKeyframeTwo() {
val constraintSet = ConstraintSet()
constraintSet.load(this, R.layout.frame_two)
TransitionManager.beginDelayedTransition(constraintLayout!!)
constraintSet.applyTo(constraintLayout)
}
}
3、使用代码方式
If you think the above way of using two layouts is not good,It can also be controlled using code,But the code will be more redundant
var constraintLayout: ConstraintLayout ?= null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
constraintLayout = findViewById(R.id.constraint_layout)
lifecycleScope.launch {
delay(1000)
animateToKeyframeOne()
}
}
private fun animateToKeyframeOne(){
val constraintSet = ConstraintSet()
constraintSet.clone(constraintLayout)
// constraintSet.clear(R.id.title) //Clear previous constraints
constraintSet.centerHorizontally(R.id.title, ConstraintSet.PARENT_ID)
//这个表示了R.id.title距离ConstraintSet.PARENT_ID The top distance is 100,后面的marginAccording to the second parameter, it is the margin from the other side
constraintSet.connect(R.id.title,ConstraintSet.TOP,ConstraintSet.PARENT_ID,ConstraintSet.TOP, 100)
TransitionManager.beginDelayedTransition(constraintLayout!!)
constraintSet.applyTo(constraintLayout)
}
三、参考链接
使用 ConstraintLayout 构建自适应界面 | Android 开发者 | Android Developers | 关键帧动画
[ConstraintSet](ConstraintSet | Android Developers)
[Android ConstraintLayout ConstraintSet动态布局](Android ConstraintLayout ConstraintSet动态布局_赵彦军的博客-CSDN博客)
边栏推荐
- Triton部署mmdeploy导出的TensorRT模型失败篇
- 有趣的USB接口和颜色分类
- unicloud 腾讯云 上传文件 Have no access right to the storage uniapp
- 创建数据库报错--MySQL server is running with the --super-read-only option
- 【C# - 爬虫】使用Selenium实现爬虫,获取近七天天气信息(包含完整代码)
- idea使用@Autowired注解爆红原因及解决方法
- 开发小技巧 navicate如何点击单元格显示全部的文本内容或通过图像查看内容
- 专属程序员的浪漫七夕
- ubuntu18.04安装redis教程
- 海康VisionMaster与西门子Smart 200进行S7通信
猜你喜欢
随机推荐
idea使用@Autowired注解爆红原因及解决方法
Transform 相对位置变换,坐标系转换
Jenkins pipeline 自动部署实践
千古第一文人苏轼的众CP
打破千篇一律,DIY属于自己独一无二的商城
10个程序员可以接私活的平台和一些建议,赚麻...
卷积神经网络CNN
两日总结四
MySQL - Row size too large (> 8126). Changing some columns to TEXT or BLOB
创建数据库报错--MySQL server is running with the --super-read-only option
MySQL内存淘汰策略
NelSon:一款新的适配matlab编程语法的编程工具
ES6新语法:symbol,map容器
likeshop单商户高级版企业源码发布了新的版本1.8.1
unity 循环选择器
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
两日总结七
【C# - 方法封装】数据转换
简析强制缓存和协商缓存
反序列化字符逃逸漏洞之