<com.xj.maxlayout.MaxLayout
android:layout_width=“match_parent”
android:layout_height=“100dp”
android:layout_marginTop=“15dp”
android:background=“@android:color/holo_blue_light”
app:ml_maxWidth=“200dp”>
<TextView
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_gravity=“center”
android:padding=“10dp”
android:text=“指定最大宽度,指定最大宽度,指定最大宽度” />
</com.xj.maxlayout.MaxLayout>
<com.xj.maxlayout.MaxLayout
android:layout_width=“200dp”
android:layout_height=“match_parent”
android:layout_marginTop=“15dp”
android:background=“@android:color/holo_blue_light”
app:ml_maxheight=“200dp”>
<TextView
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:layout_gravity=“center”
android:gravity=“center”
android:padding=“10dp”
android:text=“指定最大高度” />
</com.xj.maxlayout.MaxLayout>
<com.xj.maxlayout.MaxLayout
android:layout_width=“wrap_content”
android:layout_height=“match_parent”
android:layout_marginTop=“15dp”
android:background=“@android:color/holo_blue_light”
app:ml_maxWidth=“200dp”
app:ml_maxheight=“150dp”>
<TextView
android:layout_width=“match_parent”
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>
[外链图片转存中…(img-6KDKARyi-1712363901860)]
当然,也可以同时指定比例和最大宽度,高度。
<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>
[外链图片转存中…(img-ZcXeFYy5-1712363901861)]
原理其实很简单,对自定义 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);
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
文末
那么对于想坚持程序员这行的真的就一点希望都没有吗?
其实不然,在互联网的大浪淘沙之下,留下的永远是最优秀的,我们考虑的不是哪个行业差哪个行业难,就逃避掉这些,无论哪个行业,都会有他的问题,但是无论哪个行业都会有站在最顶端的那群人。我们要做的就是努力提升自己,让自己站在最顶端,学历不够那就去读,知识不够那就去学。人之所以为人,不就是有解决问题的能力吗?挡住自己的由于只有自己。
Android希望=技能+面试
- 技能
- 面试技巧+面试题
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
行业难,就逃避掉这些,无论哪个行业,都会有他的问题,但是无论哪个行业都会有站在最顶端的那群人。我们要做的就是努力提升自己,让自己站在最顶端,学历不够那就去读,知识不够那就去学。人之所以为人,不就是有解决问题的能力吗?挡住自己的由于只有自己。
Android希望=技能+面试
- 技能
[外链图片转存中…(img-jLRnrldh-1712363901862)] - 面试技巧+面试题
[外链图片转存中…(img-KelloF3q-1712363901862)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!