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立即变化

    
    
  • 相关阅读:
    07java基础知识
    06java基础知识
    我们都忽略了Html5的力量,如果只看成一种技术就大错特错了!
    “微信应用号对行业影响”之一,app开发速来围观
    App开发中甲乙方冲突会闹出啥后果?H5 APP 开发可以改变现状吗
    开发APP不搞清楚这20个问题,必然沦为一场灾难
    H5 App设计者需要注意的21条禁忌
    H5 APP开发必读,20个你不知道的Html5新特征和窍门
    H5 App如此强悍,要降薪的恐怕已不只是iOS程序员
    关于APP,原生和H5开发技术的争论
  • 原文地址:https://www.cnblogs.com/adamli/p/14622092.html
Copyright © 2011-2022 走看看