这几天心情不怎么好,加上上班的时候修改一堆的BUG,感觉整个人都不好了!但是我告诉自己不能这样的生活,只能把所有的情绪都放在文章中吧!希望能通过写文章让自己变得平静。什么事情都会过去的!
今天我们说点什么呢?ConstraintLayout之前在博客中讲过,当时只怪自己年少不懂事,竟不知道这东西的神奇?其实我觉得你也不知道!!!今天修改BUG百度的时候,居然看到ConstraintLayout居然能实现一些相应的动画!感觉好神奇啊。。。为什么我之前不知道呢!来,车不开起来,永远不知道稳不稳!效果图走起
在这里劝大家简单的动画可以使用这个动画效果,动画复杂的话,你还是放弃吧!真心的!!!
本文知识点
- ConstraintLayout怎么实现动画
- ConstraintSet一些API的说明
- ConstraintLayout动画的一些案例
没错,今天就讲这么一点。其实我发现一件事情,每篇文章的阅读时间不要超过10分钟,否则你也读不下去,我写着也累!!!其实这个动画效果还可以!而且实现起来还是很简单的!主要是简单。。。哈哈
1. ConstraintLayout怎么实现动画
其实我个人的理解就是通过两个快照,然后系统替你算相应的移动,然后实现中间的过渡,所以你要做的只是实现两个快照!快照是通过ConstraintSet实现的,切换的中间添加上动画的设置,否则效果过于生硬!其实感觉原理就这么多,个人感觉而已,不对的地方还请大神们指出!!!
2. ConstraintSet一些API的说明
其实动画都是基于这个类实现的!所以这里说一些常用的API,这样在后面的讲解也比较方便!
- clone(ConstraintLayout constraintLayout) 克隆当前的状态,也就相当于保存快照!
- applyTo(ConstraintLayout constraintLayout) 过渡到当前的快照,也就是你对constraintLayout设置了一些改变,这个方法就是过渡到设置后的效果
- centerHorizontally(int viewId, int toView) 移动到水平中心,如果你在水平方向上有Margin可能会有偏差
- centerVertically(int viewId, int toView) 移动到垂直中心,不过这个注意,如果你在垂直方向上有Margin可能会有偏差
- clear(int viewId) 清除相应的控件
- constrainWidth(int viewId, int width) 设置宽度
- constrainHeight(int viewId, int height) 设置高度
- connect(int startID, int startSide, int endID, int endSide, int margin)
- 参数1:要添加View的ID
- 参数2:约束条件
- 参数3:相应的锚点
- 参数4:相对于锚点的状态
- 参数5:边距
3. ConstraintLayout动画的一些案例
其实这个动画,你只要记住两个状态就可以,一个是开始的状态,一个是结束的状态,中间的过渡ConstraintSet会帮你实现的!所以重点就是通过代码设置设置默认的状态而已!!!所以我们从简倒难!先实现开始布局的那个效果!
3.1 最简单的动画
先实现布局(如果对ConstraintLayout不了解的可以看看我之前写的Android中布局的优化 就能理解了!)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/constraint"
tools:context="com.jinlong.constraint.MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="52dp"
android:layout_marginStart="52dp"
android:layout_marginTop="69dp"
android:background="@color/colorAccent"
android:text="Button 1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="136dp"
android:background="@color/colorPrimaryDark"
android:text="Button 2"
app:layout_constraintHorizontal_bias="0.571"
app:layout_constraintLeft_toRightOf="@+id/button1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="88dp"
android:layout_marginStart="88dp"
android:layout_marginTop="102dp"
android:background="@android:color/holo_red_dark"
android:text="Button 3"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button1" />
<Button
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#223344"
android:text="APPLY"
android:onClick="applyClick"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/btn2" />
<Button
android:id="@+id/btn2"
android:onClick="resetClick"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#098765"
android:text="RESET"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toRightOf="@id/btn1"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
复制代码
整个布局文件就是这样子的了!这个也就是相当于最原始的状态了!效果大概就是这样的了!
实现动画效果
其实别觉得代码多多,其实主要代码就几行,最多不超过10行!
- 创建ConstraintSet对象实现快照
private ConstraintSet resetConstraintSet = new ConstraintSet();
private ConstraintSet applyConstraintSet = new ConstraintSet();
复制代码
这两个两个是实现开始的快照,因为Reset的话还要变化回来的!
- 记录开始的快照
applyConstraintSet.clone(mConstraintLayout);
resetConstraintSet.clone(mConstraintLayout);
复制代码
- 实现动画
//这个是设置相应的渐变动画的
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
TransitionManager.beginDelayedTransition(mConstraintLayout);
applyConstraintSet.setMargin(R.id.button1, ConstraintSet.START, 0);
applyConstraintSet.setMargin(R.id.button1, ConstraintSet.END, 0);
applyConstraintSet.setMargin(R.id.button2, ConstraintSet.START, 0);
applyConstraintSet.setMargin(R.id.button2, ConstraintSet.END, 0);
applyConstraintSet.setMargin(R.id.button3, ConstraintSet.START, 0);
applyConstraintSet.setMargin(R.id.button3, ConstraintSet.END, 0);
/*
* 设置相应居中位置的
* 参数1:相应控件
* 参数2:父控件
* */
applyConstraintSet.centerHorizontally(R.id.button1, R.id.constraint);
applyConstraintSet.centerHorizontally(R.id.button2, R.id.constraint);
applyConstraintSet.centerHorizontally(R.id.button3, R.id.constraint);
applyConstraintSet.applyTo(mConstraintLayout);
复制代码
记得我上面说过如果你在竖直或者水平方向上有外边距的话,居中位置会有偏差吗?上面的代码就是重新设置的相应的外边距为0,然后把三个控件同时居中!最后调用了applyTo就能实现开头说的那个动画了!是不是很简单,如果你像还原的话,那么开始创建的两个快照就起到作用了,因为之前已经记录的相应的快照,所以这里直接调用resetConstraintSet.applyTo(mConstraintLayout);
就能直接还原了!这里有个问题,如果你不添加TransitionManager.beginDelayedTransition(mConstraintLayout);
的话,位移是有的,但是十分的生硬,但是这个方法是在API19以上才生效的,所以这里我做了相应的判断!!!
完整代码
public class MainActivity extends AppCompatActivity {
//创建两个ConstraintSet对象(这个对象能记录我们Constraint控件的所有内容)
private ConstraintSet resetConstraintSet = new ConstraintSet();
private ConstraintSet applyConstraintSet = new ConstraintSet();
private ConstraintLayout mConstraintLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mConstraintLayout = findViewById(R.id.constraint);
//克隆当前的状态
applyConstraintSet.clone(mConstraintLayout);
resetConstraintSet.clone(mConstraintLayout);
}
public void applyClick(View view) {
//这个是设置相应的渐变动画的
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
TransitionManager.beginDelayedTransition(mConstraintLayout);
//设置居中,还有水平的居中方式哦
applyConstraintSet.setMargin(R.id.button1, ConstraintSet.START, 0);
applyConstraintSet.setMargin(R.id.button1, ConstraintSet.END, 0);
applyConstraintSet.setMargin(R.id.button2, ConstraintSet.START, 0);
applyConstraintSet.setMargin(R.id.button2, ConstraintSet.END, 0);
applyConstraintSet.setMargin(R.id.button3, ConstraintSet.START, 0);
applyConstraintSet.setMargin(R.id.button3, ConstraintSet.END, 0);
/*
* 设置相应居中位置的
* 参数1:相应控件
* 参数2:父控件
* */
applyConstraintSet.centerHorizontally(R.id.button1, R.id.constraint);
applyConstraintSet.centerHorizontally(R.id.button2, R.id.constraint);
applyConstraintSet.centerHorizontally(R.id.button3, R.id.constraint);
applyConstraintSet.applyTo(mConstraintLayout);
}
public void resetClick(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
TransitionManager.beginDelayedTransition(mConstraintLayout);
resetConstraintSet.applyTo(mConstraintLayout);
}
}
复制代码
3.2 布局文件的实现动画
对你没有看错,这里可以加载两个布局文件进行动画效果!其实开始我觉的这个效果没有什么用处,但是后来感觉还是没有什么用处,感觉不如转场动画过渡的流畅!而已一个很致命的问题就是每次转换的时候,文字都会很大的变化!感觉体验不怎么好!但是有些简单的动画感觉应该还是可以的!!!
先看下效果,如果不感兴趣就可以跳过了!!!
这个感觉ConstraintLayout动画尽量纯水平的移动,或者纯竖直移动,尽量不要带有问题,否则效果不是很好!!!
上代码: 布局文件1.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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/main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@mipmap/icon"
app:layout_constraintHorizontal_weight="2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/tv_des" />
<TextView
android:id="@+id/tv_des"
android:layout_width="0dp"
android:layout_height="0dp"
android:ellipsize="end"
android:paddingBottom="10dp"
android:text="这是图片的描述但是只能显示两行知道吗剩下的我都准备省略了,省略的部分我觉得会用省略号去显示这是图片的描述但是只能显示两行知道吗剩下的我都准备省略了,省略的部分我觉得会用省略号去显示这是图片的描述但是只能显示两行知道吗剩下的我都准备省略了,省略的部分我觉得会用省略号去显示这是图片的描述但是只能显示两行知道吗剩下的我都准备省略了,省略的部分我觉得会用省略号去显示"
app:layout_constraintBottom_toTopOf="@id/tv_all"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toRightOf="@id/iv_icon"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_weight="1" />
<TextView
android:id="@+id/tv_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onApplyClick"
android:text="点我显示全部内容"
android:textColor="#0d77e1"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="@id/iv_icon"
app:layout_constraintLeft_toRightOf="@id/iv_icon"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#223340"
android:gravity="center"
android:visibility="visible"
android:textColor="@android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/iv_icon"
app:layout_constraintVertical_weight="1" />
</android.support.constraint.ConstraintLayout>
复制代码
布局文件2
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@mipmap/icon"
app:layout_constraintHorizontal_weight="2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/tv_des" />
<TextView
android:id="@+id/tv_des"
android:layout_width="match_parent"
android:layout_height="0dp"
android:ellipsize="end"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="这是图片的描述但是只能显示两行知道吗剩下的我都准备省略了,省略的部分我觉得会用省略号去显示这是图片的描述但是只能显示两行知道吗剩下的我都准备省略了,省略的部分我觉得会用省略号去显示这是图片的描述但是只能显示两行知道吗剩下的我都准备省略了,省略的部分我觉得会用省略号去显示这是图片的描述但是只能显示两行知道吗剩下的我都准备省略了,省略的部分我觉得会用省略号去显示"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/iv_icon"
app:layout_constraintVertical_weight="1" />
<TextView
android:id="@+id/tv_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onApplyClick"
android:text="点我就回去了"
android:textColor="#0d77e1"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#223340"
android:gravity="center"
android:textColor="@android:color/white"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/iv_icon"
app:layout_constraintVertical_weight="1" />
</android.support.constraint.ConstraintLayout>
复制代码
代码
public class Main2Activity extends AppCompatActivity {
private ConstraintSet start = new ConstraintSet();
private ConstraintSet end = new ConstraintSet();
private ConstraintLayout mConstraintLayout;
private boolean isSecond = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
mConstraintLayout = findViewById(R.id.main);
start.clone(mConstraintLayout);
end.clone(this, R.layout.activity_main2);
}
public void onApplyClick(View view) {
if (!isSecond) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
TransitionManager.beginDelayedTransition(mConstraintLayout);
end.applyTo(mConstraintLayout);
isSecond = true;
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
TransitionManager.beginDelayedTransition(mConstraintLayout);
start.applyTo(mConstraintLayout);
isSecond = false;
}
}
}
复制代码
效果和上面是一样的,但是感觉这个动画真的很鸡肋,并没有什么卵用!写本片文章纯属好奇,想看看效果。看到效果之后我觉得真的没有什么新奇的!还不如之前的转场动画呢?但是做一些简单位移动画的话,还是比较省事的!最起码代码量少了不少!好了就到这里了!安,亲们!!!
特别感谢 siegen的[译]Constraint Layout 动画 |动态 Constraint |用 Java 实现的 UI(这到底是什么)[第三部分]