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

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