ImageView剖析一(从源码的角度理解ImageView)

ImageView是Android开发中使用非常最频繁的控件之一(另一个使用频繁的控件是TextView),可是我们真的了解它吗?还是我们只是使用它可以满足目前的需求?如果不知道它的工作原理,我们如何能最大程度的合理使用它呢?带着这个问题我们很有必要来剖析它:
先来看看这个类的位置:android.widget.ImageView,由源码可知,继承自View:ImageView extends View
ImageView的官方描述是: Displays an arbitrary image, such as an icon. The
ImageView class can load images from various sources (such as resources or
content providers), takes care of computing its measurement from the image sothat it can be used in any layout manager, and provides various display options such as scaling and tinting.
大致意思是:显示任意图像,如图标。ImageView类可以从各种资源(如资源或内容提供者),负责图像的测量,以便它可用于任何布局管理器,并提供各种显示选项如缩放和着色。
知道了它的定义,要使用它,那我们得知道它有哪些特点,也就是我们平时所说的属性:
观察源码可知ImageView属性如下:
* @attr ref android.R.styleable#ImageView_adjustViewBounds
* @attr ref android.R.styleable#ImageView_src
* @attr ref android.R.styleable#ImageView_maxWidth
* @attr ref android.R.styleable#ImageView_maxHeight
* @attr ref android.R.styleable#ImageView_tint
* @attr ref android.R.styleable#ImageView_scaleType
* @attr ref android.R.styleable#ImageView_cropToPadding

属性一:adjustViewBounds
源码如是说:when ImageView is adjusting its bounds to preserve the aspect ratio
of its drawable(当ImageView调整时,保持其宽高比),从源码定义中可读,该属性值为boolean类
型,默认为false
private boolean mAdjustViewBounds = false;
setAdjustViewBounds(a.getBoolean(R.styleable.ImageView_adjustViewBounds, false))

属性二:src
通过阅读源码,我们并没有找到src这个字段,也没有找到对应的set和get方法,那它存在的理由是什么呢?
别急,我们再来看:
final Drawable d = a.getDrawable(R.styleable.ImageView_src);
if (d != null) {
setImageDrawable(d);
}
由此可知,src属性并没有真正给我们的ImageView设置图片资源,而是通过它我们去资源文件中读到相应的
Drawable文件,然后通过setImageDrawable方法去设置它。

属性三:maxWidth
源码如是说:An optional argument to supply a maximum width for this view. Only
valid if{@link #setAdjustViewBounds(boolean)} has been set to true(提高该视图
最大宽度的可选参数,当adjustViewBounds为true时)

属性四:maxHeight
源码如是说:An optional argument to supply a maximum height for this view. Only
valid if{@link #setAdjustViewBounds(boolean)} has been set to true(提高该视图
最大宽度的可选参数,当adjustViewBounds为true时)

属性五:tint
源码如是说: Applies a tint to the image drawable. Does not modify the current
tint(给一个Iamge Drawable去着色,不修改当前的色彩)
Subsequent calls to {@link #setImageDrawable(Drawable)} will automatically
mutate the drawable and apply the specified tint and tint mode using
{@link Drawable#setTintList(ColorStateList)}(随后使用setIamgeDrawable方法,去给
drawable设置指定的颜色)

属性六:scaleType
源码如是说:Controls how the image should be resized or moved to match the size
of this ImageView(控制Image去适应调整的大小)
取值如下:
MATRIX:Scale using the image matrix when drawing(绘图时使用图像矩阵缩放)
android:scaleType="matrix"
FIT_XY:Scale the image using {@link Matrix.ScaleToFit#FILL}
Matrix.ScaleToFit#FILL:Scale in X and Y independently, so that src matches
dst exactly(缩放时X和Y互不影响,即不按比例缩放)
android:scaleType="fitXy"
FIT_START:Scale the image using {@link Matrix.ScaleToFit#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(按原有的纵横比进行拉伸,直到视图的
某一条边完全重合,使拉伸结果对其到左部和顶部)
android:scaleType="fitStart"
FIT_CENTER:Scale the image using {@link Matrix.ScaleToFit#CENTER}
      Matrix.ScaleToFit#CENTER: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. The result
      is centered inside dst(按比例进行拉伸,直到视图的某一条边完全重合,并将 拉伸后的结果显示在视图中间)
android:scaleType="fitCenter"
FIT_END: Scale the image using {@link Matrix.ScaleToFit#END}
Matrix.ScaleToFit#END: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. END aligns the result to
the right and bottom edges of dst(按比例进行拉伸,直到视图的某一条边完全重合,
并将拉伸后的结果显示在视图底部)
android:scaleType="fitEnd"
CENTER:Center the image in the view, but perform no scaling(无缩放,按原图大小
     显示图片,当图片宽高大于View的宽高时,截取图片中间部分显示)
android:scaleType="fitEnd"
CENTER_CROP:Scale the image uniformly (maintain the image’s aspect ratio) so
that both dimensions (width and height) of the image will be equal
to or larger than the corresponding dimension of the view (minus
padding). The image is then centered in the view(按比例均匀缩放,直至充满
视图的宽高,并显示在视图的中间)
android:scaleType="centerCrop"
CENTER_INSIDE:Scale the image uniformly (maintain the image’s aspect ratio) so
that both dimensions (width and height) of the image will be equal
to or less than the corresponding dimension of the view(minus
padding). The image is then centered in the view.(当View的宽高>=
图片的宽高时,图片居中显示原大小反之将原图按比例缩放至View的宽高居中显示)

以上我们从源码的角度更深的认识了ImageView及它的属性。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值