最后
写到这里也结束了,在文章最后放上一个小小的福利,以下为小编自己在学习过程中整理出的一个学习思路及方向,从事互联网开发,最主要的是要学好技术,而学习技术是一条慢长而艰苦的道路,不能靠一时激情,也不是熬几天几夜就能学好的,必须养成平时努力学习的习惯,更加需要准确的学习方向达到有效的学习效果。
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
android:layout_height=“wrap_content”
android:layout_gravity=“center”
android:padding=“10dp”
android:text=“同时指定最大宽度和最大高度” />
</com.xj.maxlayout.MaxLayout>
指定宽高比
指定宽高比,我们需要设置两个属性,ml_ratio_standard 和 ml_ratio。ml_ratio_standard 有两个值,w_h 代表已宽度为基准,h_w 代表已高度为基准。
比如,我们要指定高度是宽度的某个比例的时候,如,高度是宽度的两倍,可以这样写
<com.xj.maxlayout.MaxLayout
android:id=“@+id/ml_1”
android:layout_width=“100dp”
android:layout_height=“wrap_content”
android:background=“@color/colorAccent”
app:ml_ratio=“2.0”
app:ml_ratio_standard=“w_h”>
<ImageView
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:scaleType=“centerCrop”
android:src=“@mipmap/jsy03” />
</com.xj.maxlayout.MaxLayout>
比如,我们要指定宽度是高度的某个比例的时候,如,宽度是高度的 0.8,可以这样写
<com.xj.maxlayout.MaxLayout
android:id=“@+id/ml_2”
android:layout_width=“match_parent”
android:layout_height=“200dp”
android:layout_marginLeft=“20dp”
android:layout_toRightOf=“@id/ml_1”
android:background=“@android:color/holo_blue_light”
app:ml_ratio=“0.8”
app:ml_ratio_standard=“h_w”>
<ImageView
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:scaleType=“centerCrop”
android:src=“@mipmap/jsy04” />
</com.xj.maxlayout.MaxLayout>
当然,也可以同时指定比例和最大宽度,高度。
<com.xj.maxlayout.MaxLayout
android:id=“@+id/ml_03”
android:layout_width=“match_parent”
android:layout_height=“220dp”
android:layout_below=“@id/ml_1”
android:layout_marginTop=“15dp”
android:background=“@android:color/holo_blue_light”
app:ml_maxWidth=“150dp”>
<ImageView
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:scaleType=“centerCrop”
android:src=“@mipmap/jsy05” />
</com.xj.maxlayout.MaxLayout>
原理其实很简单,对自定义 View 有基本了解的人都知道,View 的宽度和高度,是在 onMeasure 方法中进行测量的,他们的大小受 MeasureSpec 的影响。既然如此,那么我们在继承 FrameLayout,重写它的 onMeasure 方法。在 onMeasure 方法中根据我们指定的最大宽度,高度和比例对 MeasureSpec 进行调整即可。
思路大概如下
-
没有设置最大宽度,高度,宽高比例,不需要调整,直接返回
-
先拿到原来的 mode 和 size,暂存起来
-
根据宽高的比例进行相应的调整
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// 是否设置了比例
boolean setRatio = isSetRatio();
// 没有设置最大宽度,高度,宽高比例,不需要调整,直接返回
if (mMaxWidth <= DEF_VALUE && mMaxHeight <= DEF_VALUE && !setRatio) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
return;
}
// 拿到原来宽度,高度的 mode 和 size
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
Log.d(TAG, “origin onMeasure: widthSize =” + widthSize + "heightSize = " + heightSize);
if (mRatioStandrad == W_H) { // 当模式已宽度为基准
widthSize = getWidth(widthSize);
if (mRatio >= 0) {
heightSize = (int) (widthSize * mRatio);
}
heightSize = getHeight(heightSize);
int maxHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY);
int maxWidthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize, widthMode);
super.onMeasure(maxWidthMeasureSpec, maxHeightMeasureSpec);
} else if (mRatioStandrad == H_W) { // 当模式已高度为基准
heightSize = getHeight(heightSize);
if (mRatio >= 0) {
widthSize = (int) (heightSize * mRatio);
}
widthSize = getWidth(widthSize);
int maxHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, heightMode);
int maxWidthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);
super.onMeasure(maxWidthMeasureSpec, maxHeightMeasureSpec);
} else { // 当没有设定比例的时候
widthSize = getWidth(widthSize);
heightSize = getHeight(heightSize);
int maxHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, heightMode);
int maxWidthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize, widthMode);
super.onMeasure(maxWidthMeasureSpec, maxHeightMeasureSpec);
}
Log.d(TAG, “adjust onMeasure: widthSize =” + widthSize + "heightSize = " + heightSize);
}
结尾
最后小编想说:不论以后选择什么方向发展,目前重要的是把Android方面的技术学好,毕竟其实对于程序员来说,要学习的知识内容、技术有太多太多,要想不被环境淘汰就只有不断提升自己,从来都是我们去适应环境,而不是环境来适应我们!
当程序员容易,当一个优秀的程序员是需要不断学习的,从初级程序员到高级程序员,从初级架构师到资深架构师,或者走向管理,从技术经理到技术总监,每个阶段都需要掌握不同的能力。早早确定自己的职业方向,才能在工作和能力提升中甩开同龄人。
想要拿高薪实现技术提升薪水得到质的飞跃。最快捷的方式,就是有人可以带着你一起分析,这样学习起来最为高效,所以为了大家能够顺利进阶中高级、架构师,我特地为大家准备了一套高手学习的源码和框架视频等精品Android架构师教程,保证你学了以后保证薪资上升一个台阶。
当你有了学习线路,学习哪些内容,也知道以后的路怎么走了,理论看多了总要实践的。
高级UI,自定义View
UI这块知识是现今使用者最多的。当年火爆一时的Android入门培训,学会这小块知识就能随便找到不错的工作了。
不过很显然现在远远不够了,拒绝无休止的CV,亲自去项目实战,读源码,研究原理吧!
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
oid入门培训,学会这小块知识就能随便找到不错的工作了。
不过很显然现在远远不够了,拒绝无休止的CV,亲自去项目实战,读源码,研究原理吧!
[外链图片转存中…(img-0sI9CDTh-1715845972497)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!