ConstraintLayout动画实现

前言
最近在无意中看到一篇关于ConstraintLayout的文章,文章着重介绍了最新的ConstraintLayout支持实现动画效果。于是我参照文章的内容,实现的动画效果如下:

是不是炫酷屌炸天!关键的是那么复杂的动画效果仅仅用几行代码!!!!
实现步骤
1.把我们的ConstaintLayout的版本是升级到1.0.0-beta4’

compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'

2.添加transition的依赖。(这个很关键,不能漏掉哦!)

compile 'com.android.support:transition:25.1.0'

3.新建两个布局文件,(注意要用ConstarintLayout布局),一个为执行动画前的布局,另一个是执行动画后的布局。如我的布局文件

执行动画前的布局

<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:orientation="vertical"
    android:id="@+id/constraint_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:text="Apply"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_apply"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintRight_toLeftOf="@+id/guideline2"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp" />

    <Button
        android:text="Reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_reset"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginEnd="16dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginRight="16dp"
        app:layout_constraintLeft_toLeftOf="@+id/guideline2" />
    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline2"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5052083"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="194dp" />

    <ImageView
        android:layout_width="96dp"
        android:layout_height="96dp"
        app:srcCompat="@mipmap/ic_launcher"
        android:id="@+id/iv_picture"
        android:layout_marginStart="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="56dp"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:text="CheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteY="23dp"
        android:id="@+id/checkBox"
        android:layout_marginStart="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="16dp" />

    <TextView
        android:text="TextView1234444444444443232424334444344447758475353\nfiefjifhfehfuerieurieurif12fjeifjiejfeiffjeifjfjeijfifjeshfehfeufjieifjifjiejffjeifjifjifjiefeifjeifjeijfeifjeifjeijfiejfeifjeifjeifjeijfeifjeifjeifjeifjeifjeifjeifjeifjeifjiejfiefeijfeifjiejfeijfeijfiejfiejfiefjiefjiefjeifjeifjeifjeijfeijfeifjeijfiejfeifjeifjeifjeifji"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/tv_content"
        android:layout_marginStart="32dp"
        app:layout_constraintTop_toTopOf="@+id/checkBox"
        app:layout_constraintLeft_toRightOf="@+id/checkBox"
        android:layout_marginLeft="32dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginRight="16dp"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"
        android:layout_marginBottom="8dp" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline3"
        android:orientation="horizontal"
        tools:layout_editor_absoluteY="154dp"
        tools:layout_editor_absoluteX="0dp"
        app:layout_constraintGuide_end="357dp" />

    <Button
        android:text="Button"
        android:layout_width="368dp"
        android:layout_height="wrap_content"
        android:id="@+id/btn_content"
        android:layout_marginTop="64dp"
        app:layout_constraintTop_toBottomOf="@+id/tv_content"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent" />
android.support.constraint.ConstraintLayout>
执行动画后的布局

<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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_margin="8dp"
        android:text="Apply"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_apply"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintRight_toLeftOf="@+id/guideline2"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp" />

    <Button
        android:layout_margin="8dp"
        android:text="Reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_reset"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginEnd="16dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginRight="16dp"
        app:layout_constraintLeft_toLeftOf="@+id/guideline2" />
    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline2"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5338542"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="205dp" />

    <ImageView
        android:layout_width="96dp"
        android:layout_height="96dp"
        app:srcCompat="@mipmap/ic_launcher"
        android:id="@+id/iv_picture"
        tools:layout_editor_absoluteY="24dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginRight="16dp"
        android:layout_marginStart="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="16dp" />

    <CheckBox
        android:text="CheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/checkBox"
        tools:layout_editor_absoluteY="7dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginRight="16dp"
        app:layout_constraintLeft_toLeftOf="@+id/guideline2"
        app:layout_constraintHorizontal_bias="0.66" />

    <TextView
        android:text="TextView124444344447758475353\nfiefjifhfehfuerieurieurif12fjeifjiejfeiffjeifjfjeijfifjeshfehfeufjieifjifjiejffjeifjifjifjiefeifjeifjeijfeifjeifjeijfiejfeifjeifjeifjeijfeifjeifjeifjeifjeifjeifjeifjeifjeifjiejfiefeijfeifjiejfeijfeijfiejfiejfiefjiefjiefjeifjeifjeifjeijfeijfeifjeijfiejfeifjeifjeifjeifji"
        android:layout_width="226dp"
        android:layout_height="wrap_content"
        android:id="@+id/tv_content"
        app:layout_constraintRight_toLeftOf="@+id/guideline2"
        app:layout_constraintLeft_toLeftOf="@+id/guideline2"
        app:layout_constraintTop_toTopOf="@+id/guideline3" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline3"
        android:orientation="horizontal"
        tools:layout_editor_absoluteY="130dp"
        tools:layout_editor_absoluteX="0dp"
        app:layout_constraintGuide_end="381dp" />

    <Button
        android:layout_margin="16dp"
        android:text="Button"
        android:layout_width="0dp"
        android:layout_height="120dp"
        android:id="@+id/btn_content"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_content"
        app:layout_constraintHorizontal_bias="0.0" />
android.support.constraint.ConstraintLayout>

注意:务必保持两个布局文件中对应控件的Id一致,要不然没有动画效果
4.在Activity中设置Java代码。如下:

public class MainActivity extends AppCompatActivity {


    @InjectView(R.id.btn_apply)
    Button btnApply;
    @InjectView(R.id.btn_reset)
    Button btnReset;
    @InjectView(R.id.guideline2)
    Guideline guideline2;
    @InjectView(R.id.iv_picture)
    ImageView ivPicture;
    @InjectView(R.id.checkBox)
    CheckBox checkBox;
    @InjectView(R.id.tv_content)
    TextView tvContent;
    @InjectView(R.id.guideline3)
    Guideline guideline3;
    @InjectView(R.id.btn_content)
    Button btnContent;
    @InjectView(R.id.constraint_layout)
    ConstraintLayout constraintLayout;
    private ConstraintSet mConstraintSet1, mConstraintSet2, mConstraintReset;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animation_normal);
        ButterKnife.inject(this);
        mConstraintSet1 = new ConstraintSet();
        mConstraintSet2 = new ConstraintSet();
        mConstraintSet1.clone(constraintLayout);
        mConstraintSet2.clone(this,R.layout.animation_execute);
    }

    @OnClick({R.id.btn_apply, R.id.btn_reset})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_apply:
                TransitionManager.beginDelayedTransition(constraintLayout);
                mConstraintSet2.applyTo(constraintLayout);
                break;
            case R.id.btn_reset:
                TransitionManager.beginDelayedTransition(constraintLayout);
                mConstraintSet1.applyTo(constraintLayout);
                break;
        }
    }

其实这里最为关键的是ConstraintSet这个类的应用 。关于这个类的介绍请戳ConstaintSet,就是类撑起ConstaintLayout实现动画效果的一片天。。。哈哈哈哈!!

  mConstraintSet1 = new ConstraintSet();
        mConstraintSet2 = new ConstraintSet();
        mConstraintSet1.clone(constraintLayout);//正常状态
        mConstraintSet2.clone(this,R.layout.animation_execute);执行动画后的状态

首先我们创建两个变量mConstraintSet1,(用于恢复正常状态)mConstraintSet2(用于执行动画状态),初始化变量。
然后分别调用它们clone()的方法,把ConstraintLayout的布局内容克隆到ConstraintSet中。
需要注意的是clone(this,R.layout.animation_execute)这个方法,它的第二个参数为你要执行动画后的布局哦。。。。

最后我们在回调函数中执行如下代码即可。

执行动画代码:

    TransitionManager.beginDelayedTransition(constraintLayout);
                mConstraintSet2.applyTo(constraintLayout);

还原正常状态代码

  TransitionManager.beginDelayedTransition(constraintLayout);
                mConstraintSet1.applyTo(constraintLayout);

到此,运行代码就可以看到动画效果了。
这就是ConstraintSet的简单用法,它还可以为ConstaintLayout实现许多炫酷的动画效果,如果你有兴趣的话,可以戳这里ConstraintSet实现动画效果查看更多内容。。。。

你可能感兴趣的:(android)