ConstraintLayout2.0版本除了優(yōu)化布局性能外,還增加了一些新特性,使得開(kāi)發(fā)過(guò)程更加方便。
ImageFilterButton、ImageFilterView
ImageFilterView、ImageFilterButton分別對(duì)應(yīng)ImageView、ImageViewButton。主要用來(lái)處理圓角、色差、放大縮小的特性。
- 圓角大小app:round:取值0-size/2之間,超過(guò)就沒(méi)什么意義了,默認(rèn)0,就是方形;對(duì)于正方形來(lái)說(shuō),取值size/2就是圓形, 圓角是針對(duì)View的, 將View繪制成圓角.
- 圓角比例app:roundPercent:取值0-1之間,超過(guò)1就沒(méi)什么意義了,默認(rèn)0就是方形,1是圓形圖片。和app:round意思一樣,只不過(guò)一個(gè)是具體的大小,一個(gè)是百分比。
- 縮放app:imageZoom:放大或縮小圖片大小,比如:2表示圖片放大到原來(lái)的2倍,0.5表示圖片縮小到原來(lái)的一半。View的大小不變,只是顯示的圖片縮放了。
- 旋轉(zhuǎn)app:imageRotate:旋轉(zhuǎn)圖片的角度,比如90,表示圖片旋轉(zhuǎn)90度。View的角度和大小是不變的。
- 交叉圖app:altSrc:需要跟app:crossfade共同使用,app:crossfade取值0-1,默認(rèn)0為交叉圖完全透明,不展示。取值1交叉圖完全展示,覆蓋到src上。
- 飽和度app:saturation:float型,默認(rèn)1,取值0為灰階樣式,大于1的數(shù)值都是超飽和狀態(tài),色彩非常艷麗,有點(diǎn)辣眼睛。
- 亮度app:brightness:float型,默認(rèn)1,值越大亮度越高。
- 色溫app:warmth:float型,默認(rèn)值1,小于1是冷色調(diào),大于1是暖色調(diào)。
對(duì)比度app:contrast:float型,默認(rèn)1,取值0相當(dāng)于圖片變?nèi)冢笥?都是高對(duì)比度狀態(tài)。 - app:overlay,官方釋義:定義alt圖像是在原始圖像的頂部淡入,還是與其交叉淡入。默認(rèn)值為true。對(duì)于半透明對(duì)象設(shè)置為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相似,可以認(rèn)為是Group的強(qiáng)化版,它可以讓你在多個(gè)視圖上創(chuàng)建一個(gè)虛擬的圖層。但是,與Flow不同的是,它并不會(huì)對(duì)視圖進(jìn)行布局操作,它的使用場(chǎng)景是對(duì)多個(gè)視圖同時(shí)進(jìn)行變換。
例如,你需要對(duì)多個(gè)視圖整體進(jìn)行旋轉(zhuǎn)、平移或縮放操作,再或者說(shuō)是設(shè)置一組View的背景,那么就可以使用Layer。

Layer在布局期間會(huì)調(diào)整大小,其大小會(huì)根據(jù)其引用的所有視圖進(jìn)行調(diào)整,你可以將Layer理解為一組View的邊界矩形范圍,通過(guò)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是一個(gè)流式布局,跟ChipGroup、Chip有點(diǎn)類(lèi)似??梢灾谱鞑灰?guī)則的布局。
- app:constraint_referenced_ids 指定引用的控件id或其它Flow 的id等,也可以通過(guò)tag引用.
被引用的控件原來(lái)的方位約束會(huì)失效
按引用的順序排列 -
app:flow_wrapMode指定控件排列時(shí)自適應(yīng)方式,不同方式可用的配套屬性也不一樣。
none : 簡(jiǎn)單地把constraint_referenced_ids里面的元素組成chain,即使空間不夠
chain : 根據(jù)空間的大小和元素的大小組成一條或者多條 chain
aligned : chain類(lèi)似,但是會(huì)對(duì)齊
- android:orientation 指定Flow的方向
- Gap展示了Flow中每個(gè)元素直接的間隔,這個(gè)間隔包含horizontalGap和verticalGap兩種,你可以在原有Chain Style的基礎(chǔ)上進(jìn)行額外設(shè)置
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新增的用于切換狀態(tài)布局的一個(gè)功能,它可以根據(jù)狀態(tài)切換不同的布局文件。
首先,需要在layout下創(chuàng)建不同狀態(tài)的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>
控制狀態(tài)切換:
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
