zoukankan      html  css  js  c++  java
  • ConstraintLayout 布局和动画使用

    一、百分比布局使用Guideline


    <?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"
    android:id="@+id/layout_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
    android:id="@+id/btn_two"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="btn2"
    android:textSize="@dimen/text_size_large"
    app:layout_constraintLeft_toRightOf="@id/layout_one"
    app:layout_constraintTop_toTopOf="@id/guideline_h_1" />

    <LinearLayout
    android:id="@+id/layout_one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintLeft_toLeftOf="@id/guideline_v_1"
    app:layout_constraintTop_toTopOf="@id/guideline_h_1">

    <Button
    android:id="@+id/btn_one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/app_name"
    android:textSize="@dimen/text_size_large" />
    </LinearLayout>

    <TextView
    android:id="@+id/c_text"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="@color/colorBlue"
    app:layout_constraintLeft_toLeftOf="@id/guideline_v_1"
    app:layout_constraintTop_toBottomOf="@id/layout_one">

    </TextView>
    <!--region for guideline-->
    <androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline_h_1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.04" />

    <androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline_h_2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.5" />

    <androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline_h_3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.8" />

    <androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline_v_1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.06" />
    <!--endregion for guideline-->
    </androidx.constraintlayout.widget.ConstraintLayout>

    二、位移动画使用

    如下代码 蒋buttonOne 动画移动到guideline_h_2位置

    ConstraintSet set = new ConstraintSet();

    set.clone(mLayoutRoot);

    set.connect(R.id.layout_one, ConstraintSet.TOP, R.id.guideline_h_2, ConstraintSet.TOP);

    set.connect(R.id.btn_two, ConstraintSet.TOP, R.id.guideline_h_3, ConstraintSet.TOP);

    set.applyTo(mLayoutRoot);


    这里需要注意一下,在调用clone()方法的时候,必须保证这个父布局的所有子布局都设置了 id,不然会报错误

    R.id.layout_one R.id.btn_two 只能是mLayoutRoot 的直接子布局,不能越级嵌套。


    //如下执行动画操作,如果不调用,则立即位移
    Transition transition = new ChangeBounds();
    TransitionManager.endTransitions(mLayoutRoot);
    transition.setInterpolator(interpolator);//设置插值器
    transition.setDuration(duration); //设置动画时长
    TransitionManager.beginDelayedTransition(mLayoutRoot,transition);//执行动画

    注:如果将 mLayoutRoot 换成  mLayoutOne 则只有layout_one会执行动画,其他view立即变化

    
    
  • 相关阅读:
    mysql修改数据表名
    HDU 5742 It's All In The Mind (贪心)
    HDU 5752 Sqrt Bo (数论)
    HDU 5753 Permutation Bo (推导 or 打表找规律)
    HDU 5762 Teacher Bo (暴力)
    HDU 5754 Life Winner Bo (博弈)
    CodeForces 455C Civilization (并查集+树的直径)
    CodeForces 455B A Lot of Games (博弈论)
    CodeForces 455A Boredom (DP)
    HDU 4861 Couple doubi (数论 or 打表找规律)
  • 原文地址:https://www.cnblogs.com/adamli/p/14622092.html
Copyright © 2011-2022 走看看