Constraintlayout 2,还在等机会

上文XML布局中,所有TextView的宽高是一致的,所以看着整整齐齐,当宽高不一致时,可以进行对齐处理。个人试了一下app:flow_wrapMode="aligned"下的对齐,没啥效果,估计有默认值了吧。看看flow_wrapMode属性为nonechain情况吧。

Flow布局添加以下属性进行不同Align约束:

  • flow_verticalAlign 垂直方向对齐,取值有:topbottomcenterbaseline;
  • flow_horizontalAlign 水平方向对齐,取值有:startendcenter;

对齐方向一般与链的方向相反才可生效,例如垂直链样式,一般对齐View的左右边和中间。

简单举个例子:垂直方向顶部对齐。

「效果图:」

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传 可以看到EGF顶部对齐。

代码:

<androidx.constraintlayout.helper.widget.Flow android:id="@+id/flow" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" app:constraint_referenced_ids="tvA,tvB,tvC,tvD,tvE,tvF,tvG" app:flow_maxElementsWrap="4" app:flow_horizontalGap="30dp" app:flow_verticalGap="30dp" app:flow_wrapMode="chain" app:flow_verticalAlign="top" app:layout_constraintHorizontal_chainStyle="spread" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" />

简单的理解alignedchiannone的定制版,通过添加不同的属性定制而成。由于Flow是虚拟布局,简单理解就是约束助手,它并不会增加布局层级,却可以像正常的布局一样使用。

「其他属性」

上文的XML的布局没有设置Flow对View的组织方式(水平or 垂直),可以通过orientation属性来设置水平horizontal和垂直vertical方向,例如改为垂直方向。

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传


flow_wrapMode属性为alignedchian时,通过flow_maxElementsWrap属性控制每行最大的子View数量。例如:flow_maxElementsWrap=3

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传


flow_wrapMode属性为none时,A和G被挡住了,看不到。

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传 要A或者G可见,通过设置flow_horizontalBias属性,取值在0-1之间。前提条件是flow_horizontalStyle属性为packed才会生效。

<androidx.constraintlayout.helper.widget.Flow android:id="@+id/flow" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" app:constraint_referenced_ids="tvA,tvB,tvC,tvD,tvE,tvF,tvG" app:flow_horizontalGap="30dp" app:flow_verticalGap="30dp" app:flow_wrapMode="none" app:flow_horizontalStyle="packed" app:flow_horizontalBias="0" android:layout_marginTop="10dp" app:layout_constraintRight_toRightOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" />

「效果图:」 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传 设置flow_horizontalBias=1那么G就可以看到了。该属性还有其他类似ChainStyle的属性w玩法,具体可以实践体验。当然,也可以在flow_wrapMode属性为其他值生效。

通过不同的属性可以搭配很多不同的效果,再加上MotionLayout动画,那就更炫酷了。

二、Layer 层布局

Layer也是一个约束助手ConstraintHelper,相对Flow比较简单,常用来增加背景,或者共同动画。由于ConstraintHelper本身继承自View,跟我们自己通过View在ConstraintLayout布局中给多个View添加共同背景没什么区别,只是更方便而已。

「1、添加背景」

ImageViewTextView添加个共同背景:

「效果:」

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

「代码:」

<?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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.constraintlayout.helper.widget.Layer android:id="@+id/layer" android:layout_marginTop="50dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorPrimary" app:constraint_referenced_ids="ivImage,tvName" app:layout_constraintLeft_toLeftOf="@id/ivImage" app:layout_constraintRight_toRightOf="parent" android:padding="10dp" app:layout_constraintTop_toTopOf="parent" tools:ignore="MissingConstraints" /> <ImageView android:id="@+id/ivImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:src="@mipmap/ic_launcher_round" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/layer" /> <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="新小梦" android:textColor="#FFFFFF" android:paddingTop="5dp" app:layout_constraintLeft_toLeftOf="@id/ivImage" app:layout_constraintRight_toRightOf="@id/ivImage" app:layout_constraintTop_toBottomOf="@id/ivImage" /> </androidx.constraintlayout.widget.ConstraintLayout>

「2、共同动画」

通过属性动画给ImageView和TextView添加通过动画效果。

「效果:」

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传 「代码:」

val animator = ValueAnimator.ofFloat( 0f, 360f) animator.repeatMode=ValueAnimator.RESTART animator.duration=2000 animator.interpolator=LinearInterpolator() animator.repeatCount=ValueAnimator.INFINITE animator.addUpdateListener { layer.rotation= it.animatedValue as Float } layer.setOnClickListener { animator.start() }

对属性动画模糊的同学可以看看:Android属性动画,看完这篇够用了吧

支持:旋转、位移、缩放动画。透明效果试了一下,是针对自身的,而不是约束的View。

三、自定义ConstraintHelper

FlowLayer都是ConstraintHelper的子类,当两者不满足需求时,可以通过继承ConstraintHelper来实现想要的约束效果。

在某乎APP有这么个类似的动画广告:

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传 那么通过自定义ConstraintHelper来实现就非常简单:

class AdHelper : ConstraintHelper { constructor(context: Context?) : super(context) constructor(context: Context?,attributeSet: AttributeSet):super(context,attributeSet) constructor(context: Context?,attributeSet: AttributeSet,defStyleAttr: Int):super(context,attributeSet,defStyleAttr) override fun updatePostLayout(container: ConstraintLayout?) { super.updatePostLayout(container) val views = getViews(container) views.forEach { val anim = ViewAnimationUtils.createCircularReveal(it, 0, 0, 0f, it.width.toFloat()) anim.duration = 5000 anim.start() } } }

布局引用AdHleper

<?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:layout_width="match_parent" android:layout_height="match_parent"> <com.example.constraint.AdHelper android:layout_width="wrap_content" android:layout_height="wrap_content" app:constraint_referenced_ids="ivLogo" app:layout_constraintLeft_toLeftOf="@id/ivLogo" app:layout_constraintRight_toRightOf="@id/ivLogo" app:layout_constraintTop_toTopOf="@id/ivLogo" /> <ImageView android:id="@+id/ivLogo" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="20dp" android:adjustViewBounds="true" android:scaleType="fitXY" android:src="@mipmap/ic_logo" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>

四、ImageFilterButton

圆角图片,圆形图片怎么实现?自定义View?通过ImageFilterButton,一个属性就搞定;ImageFilterButto能做的还有更多。

看看如何实现圆角或圆形图片:

「原图:」

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传roundPercent属性设置为1,取值在0-1,由正方形向圆形过渡。

<androidx.constraintlayout.utils.widget.ImageFilterButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" app:roundPercent="1" android:src="@mipmap/ic_launcher" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />

「效果:」

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传 也可以通过设置round属性来实现:

<androidx.constraintlayout.utils.widget.ImageFilterButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:src="@mipmap/ic_launcher" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:round="50dp" />


「其他属性:」

altSrcsrc属性是一样的概念,altSrc提供的资源将会和src提供的资源通过crossfade属性形成交叉淡化效果。默认情况下,crossfade=0altSrc所引用的资源不可见,取值在0-1。 例如:

<androidx.constraintlayout.utils.widget.ImageFilterButton android:id="@+id/ivImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:src="@mipmap/ic_launcher" app:altSrc="@mipmap/ic_sun" app:crossfade="0.5" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:round="50dp" />

crossfade=0.5时,效果:

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传 crossfade=1时,效果:

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传


接下来几个属性是对图片进行调节:

warmth色温:1=neutral自然, 2=warm暖色, 0.5=cold冷色

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传 brightness亮度:0 = black暗色, 1 = original原始, 2 = twice as bright两倍亮度;这个效果不好贴图,大家自行验证;

saturation饱和度:0 = grayscale灰色, 1 = original原始, 2 = hyper saturated超饱和

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传 contrast对比:1 = unchanged原始, 0 = gray暗淡, 2 = high contrast高对比;

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

上面属性的取值都是0、1、2,不过大家可以取其他值,效果也是不一样的。 最后一个属性overlay,表示不知道怎么用,看不到没效果,大家看看评论跟我说声?

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

五、ImageFilterView

ImageFilterViewImageFilterButton的属性一模一样,只是它两继承的父类不一样,一些操作也就不一样。ImageFilterButton继承自AppCompatImageButton,也就是ImageButtion。而ImageFilterView继承自ImageView

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

六、MockView

还记得你家项目经理给你的UI原型图么?想不想回敬一下项目经理,是时候了~ 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传 MockView能简单的帮助构建UI界面,通过对角线形成的矩形+标签。例如:

<?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:layout_width="match_parent" android:layout_height="match_parent"> <androidx.constraintlayout.utils.widget.MockView android:id="@+id/first" android:layout_width="100dp" android:layout_height="100dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" /> <androidx.constraintlayout.utils.widget.MockView android:id="@+id/second" android:layout_width="100dp" android:layout_height="100dp" app:layout_constraintLeft_toRightOf="@id/first" app:layout_constraintTop_toBottomOf="@id/first" /> </androidx.constraintlayout.widget.ConstraintLayout>

「效果:」

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传 中间黑色显示的是MockViewid。通过MockView可以很好的构建一些UI思路。

七、MotionLayout

MitionLayou主要是用来实现动作动画,可以参考我的另一篇文章:Android MotionLayout动画:续写ConstraintLayout新篇章

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

img
img

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V:vip204888 备注Android获取(资料价值较高,非无偿)
img

学习福利

【Android 详细知识点思维脑图(技能树)】

其实Android开发的知识点就那么多,面试问来问去还是那么点东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。

虽然 Android 没有前几年火热了,已经过去了会四大组件就能找到高薪职位的时代了。这只能说明 Android 中级以下的岗位饱和了,现在高级工程师还是比较缺少的,很多高级职位给的薪资真的特别高(钱多也不一定能找到合适的),所以努力让自己成为高级工程师才是最重要的。

这里附上上述的面试题相关的几十套字节跳动,京东,小米,腾讯、头条、阿里、美团等公司19年的面试题。把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。

由于篇幅有限,这里以图片的形式给大家展示一小部分。

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

就能找到高薪职位的时代了。这只能说明 Android 中级以下的岗位饱和了,现在高级工程师还是比较缺少的,很多高级职位给的薪资真的特别高(钱多也不一定能找到合适的),所以努力让自己成为高级工程师才是最重要的。

这里附上上述的面试题相关的几十套字节跳动,京东,小米,腾讯、头条、阿里、美团等公司19年的面试题。把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。

由于篇幅有限,这里以图片的形式给大家展示一小部分。

[外链图片转存中…(img-rIegpxD4-1711557229844)]

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录

  • 8
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值