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>
当然,也可以同时指定比例和最大宽度,高度。
<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);
如何成为Android高级架构师!
架构师必须具备抽象思维和分析的能力,这是你进行系统分析和系统分解的基本素质。只有具备这样的能力,架构师才能看清系统的整体,掌控全局,这也是架构师大局观的形成基础。 你如何具备这种能力呢?一是来自于经验,二是来自于学习。
架构师不仅要具备在问题领域上的经验,也需要具备在软件工程领域内的经验。也就是说,架构师必须能够准确得理解需求,然后用软件工程的思想,把需求转化和分解成可用计算机语言实现的程度。经验的积累是需要一个时间过程的,这个过程谁也帮不了你,是需要你去经历的。
但是,如果你有意识地去培养,不断吸取前人的经验的话,还是可以缩短这个周期的。这也是我整理架构师进阶此系列的始动力之一。
成为Android架构师必备知识技能
对应导图的学习笔记(由阿里P8大牛手写,我负责整理成PDF笔记)
部分内容展示
《设计思想解读开源框架》
- 目录
- 热修复设计
- 插件化框架设计
《360°全方面性能优化》
- 设计思想与代码质量优化
- 程序性能优化
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
计思想与代码质量优化**
[外链图片转存中…(img-Al6vdAHm-1714330391294)]
- 程序性能优化
[外链图片转存中…(img-4ATFuTep-1714330391294)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!