Android技术知识Android开发经验谈Android开发

ConstraintLayout2.0使用

2023-01-08  本文已影响0人  奔跑吧李博

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

ImageFilterButton、ImageFilterView

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

使用示例:

原图:


加上属性:

    <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有点类似。可以制作不规则的布局。

使用示例:

    <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

上一篇 下一篇

猜你喜欢

热点阅读