ConstraintLayout2.0使用

ConstraintLayout2.0版本除了优化布局性能外,还增加了一些新特性,使得开发过程更加方便。

ImageFilterButton、ImageFilterView

ImageFilterView、ImageFilterButton分别对应ImageView、ImageViewButton。主要用来处理圆角、色差、放大缩小的特性。

  • 圆角大小app:round:取值0-size/2之间,超过就没什么意义了,默认0,就是方形;对于正方形来说,取值size/2就是圆形, 圆角是针对View的, 将View绘制成圆角.
  • 圆角比例app:roundPercent:取值0-1之间,超过1就没什么意义了,默认0就是方形,1是圆形图片。和app:round意思一样,只不过一个是具体的大小,一个是百分比。
  • 缩放app:imageZoom:放大或缩小图片大小,比如:2表示图片放大到原来的2倍,0.5表示图片缩小到原来的一半。View的大小不变,只是显示的图片缩放了。
  • 旋转app:imageRotate:旋转图片的角度,比如90,表示图片旋转90度。View的角度和大小是不变的。
  • 交叉图app:altSrc:需要跟app:crossfade共同使用,app:crossfade取值0-1,默认0为交叉图完全透明,不展示。取值1交叉图完全展示,覆盖到src上。
  • 饱和度app:saturation:float型,默认1,取值0为灰阶样式,大于1的数值都是超饱和状态,色彩非常艳丽,有点辣眼睛。
  • 亮度app:brightness:float型,默认1,值越大亮度越高。
  • 色温app:warmth:float型,默认值1,小于1是冷色调,大于1是暖色调。
    对比度app:contrast:float型,默认1,取值0相当于图片变全黑,大于1都是高对比度状态。
  • app:overlay,官方释义:定义alt图像是在原始图像的顶部淡入,还是与其交叉淡入。默认值为true。对于半透明对象设置为false。
使用示例:

原图:


加上属性:

    <androidx.constraintlayout.utils.widget.ImageFilterView
        android:id="@+id/imagefilterview"
        android:layout_width="300dp"
        android:layout_height="200dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:src="@mipmap/pic_test"
        android:scaleType="centerCrop"
        android:layout_marginTop="30dp"
        app:round="10dp"
        app:imageZoom="2"
        app:imageRotate="40"
        app:saturation="0"
        app:brightness="1.2"
        app:contrast="0.8"
        app:overlay="true"/>

属性图:


Layer

Layer作为一种新的辅助工具,用法跟Group相似,可以认为是Group的强化版,它可以让你在多个视图上创建一个虚拟的图层。但是,与Flow不同的是,它并不会对视图进行布局操作,它的使用场景是对多个视图同时进行变换。
例如,你需要对多个视图整体进行旋转、平移或缩放操作,再或者说是设置一组View的背景,那么就可以使用Layer。


Layer在布局期间会调整大小,其大小会根据其引用的所有视图进行调整,你可以将Layer理解为一组View的边界矩形范围,通过Layer,可以很方便的拿到referenced_ids指定的View的边界范围,示例代码如下所示。

    <androidx.constraintlayout.helper.widget.Layer
        android:id="@+id/layer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:constraint_referenced_ids="imagefilterview, imagefilterview2"/>
Flow

Flow是一个流式布局,跟ChipGroup、Chip有点类似。可以制作不规则的布局。

  • app:constraint_referenced_ids 指定引用的控件id或其它Flow 的id等,也可以通过tag引用.
    被引用的控件原来的方位约束会失效
    按引用的顺序排列
  • app:flow_wrapMode指定控件排列时自适应方式,不同方式可用的配套属性也不一样。
    none : 简单地把constraint_referenced_ids里面的元素组成chain,即使空间不够
    chain : 根据空间的大小和元素的大小组成一条或者多条 chain
    aligned : chain类似,但是会对齐


  • android:orientation 指定Flow的方向
  • Gap展示了Flow中每个元素直接的间隔,这个间隔包含horizontalGap和verticalGap两种,你可以在原有Chain Style的基础上进行额外设置
    app:flow_horizontalGap="30dp" 水平间隔
    app:flow_verticalGap="30dp" 垂直间隔

使用示例:

    <androidx.constraintlayout.helper.widget.Flow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:constraint_referenced_ids="imagefilterview, imagefilterview2"
        android:orientation="horizontal"
        app:flow_wrapMode="chain"
        app:flow_horizontalGap="10dp"/>
ConstraintLayoutStates

ConstraintLayoutStates是ConstraintLayout2.0新增的用于切换状态布局的一个功能,它可以根据状态切换不同的布局文件。

首先,需要在layout下创建不同状态的layout xml文件,布局文件的root id相同即可。
新建布局:

<?xml version="1.0" encoding="utf-8"?>
<ConstraintLayoutStates xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <State
        android:id="@+id/start"
        app:constraints="@layout/activity_constraint_layout_states_start" />

    <State
        android:id="@+id/loading"
        app:constraints="@layout/activity_constraint_layout_states_loading" />

    <State
        android:id="@+id/end"
        app:constraints="@layout/activity_constraint_layout_states_end" />

</ConstraintLayoutStates>

控制状态切换:

        val constraintLayout = findViewById<ConstraintLayout>(R.id.constraint_state)
        constraintLayout.loadLayoutDescription(R.xml.constraint_layout_states)
        constraintLayout.setOnClickListener {
            constraintLayout.setState(R.id.loading, 0, 0)
        }

参考:
https://mp.weixin.qq.com/s/7r_53A5wbgHpfJV_BHHglQ
https://blog.csdn.net/Mr_Tony/article/details/125133580

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章