上文XML布局中,所有TextView的宽高是一致的,所以看着整整齐齐,当宽高不一致时,可以进行对齐处理。个人试了一下app:flow_wrapMode="aligned"
下的对齐,没啥效果,估计有默认值了吧。看看flow_wrapMode
属性为none
和chain
情况吧。
给Flow
布局添加以下属性进行不同Align
约束:
flow_verticalAlign
垂直方向对齐,取值有:top
、bottom
、center
、baseline
;flow_horizontalAlign
水平方向对齐,取值有:start
、end
、center
;
对齐方向一般与链的方向相反才可生效,例如垂直链样式,一般对齐View
的左右边和中间。
简单举个例子:垂直方向顶部对齐。
「效果图:」
可以看到
E
和G
、F
顶部对齐。
代码:
<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" />
简单的理解aligned
和chian
是none
的定制版,通过添加不同的属性定制而成。由于Flow
是虚拟布局,简单理解就是约束助手,它并不会增加布局层级,却可以像正常的布局一样使用。
「其他属性」
上文的XML的布局没有设置Flow对View的组织方式(水平or 垂直),可以通过orientation
属性来设置水平horizontal
和垂直vertical
方向,例如改为垂直方向。
当flow_wrapMode
属性为aligned
和chian
时,通过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、添加背景」
给ImageView
和TextView
添加个共同背景:
「效果:」
「代码:」
<?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
Flow
和Layer
都是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" />
「其他属性:」
altSrc
和src
属性是一样的概念,altSrc
提供的资源将会和src
提供的资源通过crossfade
属性形成交叉淡化效果。默认情况下,crossfade=0
,altSrc
所引用的资源不可见,取值在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
ImageFilterView
与ImageFilterButton
的属性一模一样,只是它两继承的父类不一样,一些操作也就不一样。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>
「效果:」
中间黑色显示的是
MockView
的id
。通过MockView
可以很好的构建一些UI思路。
七、MotionLayout
MitionLayou主要是用来实现动作动画,可以参考我的另一篇文章:Android MotionLayout动画:续写ConstraintLayout新篇章
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V:vip204888 备注Android获取(资料价值较高,非无偿)
学习福利
【Android 详细知识点思维脑图(技能树)】
其实Android开发的知识点就那么多,面试问来问去还是那么点东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。
虽然 Android 没有前几年火热了,已经过去了会四大组件就能找到高薪职位的时代了。这只能说明 Android 中级以下的岗位饱和了,现在高级工程师还是比较缺少的,很多高级职位给的薪资真的特别高(钱多也不一定能找到合适的),所以努力让自己成为高级工程师才是最重要的。
这里附上上述的面试题相关的几十套字节跳动,京东,小米,腾讯、头条、阿里、美团等公司19年的面试题。把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。
由于篇幅有限,这里以图片的形式给大家展示一小部分。
网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。
就能找到高薪职位的时代了。这只能说明 Android 中级以下的岗位饱和了,现在高级工程师还是比较缺少的,很多高级职位给的薪资真的特别高(钱多也不一定能找到合适的),所以努力让自己成为高级工程师才是最重要的。
这里附上上述的面试题相关的几十套字节跳动,京东,小米,腾讯、头条、阿里、美团等公司19年的面试题。把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。
由于篇幅有限,这里以图片的形式给大家展示一小部分。
[外链图片转存中…(img-rIegpxD4-1711557229844)]
网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。