mAdjustViewBounds属性
- True when ImageView is adjusting its bounds to preserve the aspect ratio of its drawable
- 如果允许adjustViewBounds,ScaleType会强行设置为FIT_CENTER
MaxWidth/MaxHeight机制: 要生效的话,必须setAdjustViewBounds设置为true:
- E.g, 要设置一个imageView最大为100*100, 3 steps:
- set adjustViewBounds to true
- set maxWidth and maxHeight to 100
- set the height and width layout params to WRAP_CONTENT
- E.g, 要设置一个imageView最大为100*100, 3 steps:
setImageResource(int resId):
- This does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup(Bitmap的读取和解码都是在UI线程完成的,可能会有小卡顿)
ScaleType:
- MATRIX, 使用通过setImageMatrix(Matrix)设置的Matrix来**绘制(严格意义上讲,Image本身没有被Matrix改变,改变的是Canvas)**Image
- FIT_XY: 对应Matrix.ScaleToFit#FILL:
- Scale in X and Y independently, so that src matches dst exactly. This may change the aspect ratio of the src.(非等比例放大/缩小Image来完全填充整个View,因此Image的纵横比可能会被改变)
- FIT_START: 对应Matrix.ScaleToFit#START:
- Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. START aligns the result to the left and top edges of dst(等比例放大/缩小Image,同时确保Image不会超出View, 并且至少在一个维度上Image和View的尺寸是相等的, START在这里的意思是经过处理的Image会在左上和View对齐).
- FIT_CENTER: 对应Matrix.ScaleToFit#CENTER:
- 基本同FIT_START,唯一区别是Image在View中是居中的。
- FIT_END: 对应Matrix.ScaleToFit#END:
- 基本同FIT_END,唯一区别是Image在右下与View对齐。
- CENTER: Center the image in the view, but perform no scaling
- CENTER_CROP: Scale the image uniformly (maintain the image’s aspect ratio) so that both dimensions (width and height) of
ImageView的测量与绘制机制解析
最新推荐文章于 2021-05-26 03:12:04 发布
本文详细探讨了ImageView的mAdjustViewBounds属性、MaxWidth/MaxHeight机制,以及ScaleType的各种模式,包括FIT_XY、FIT_START、FIT_CENTER等。解释了setImageResource()可能导致的UI线程延迟,以及如何通过onMeasure()方法调整图像的尺寸以保持其原始比例。此外,还介绍了Drawable的解析流程和Matrix在不同ScaleType下的应用,揭示了ImageView如何在绘制时实现图片的缩放、居中等效果。
摘要由CSDN通过智能技术生成