一、設(shè)置后的效果圖
imageview設(shè)置scaleType屬性后不同值的效果
1、原圖比imageview大設(shè)置后的效果

2、原圖比imageview小設(shè)置后的效果

二、scaleType各值的含義
1、MATRIX
該模式下ImageView用戶設(shè)置的比例縮放圖片,經(jīng)過MATRIX縮放后的圖片將從ImageView的左上角開始繪制。如果縮放后的圖片大于ImageView,那么多余的部分則剪裁掉,如果小于ImageView,則不作做任何處理。
2、CENTER
該模式按圖片的原有的尺寸居中顯示,
- 當(dāng)dwidth>vwidth,dheight<vheight時,其實(shí)就是將圖片左移和向下移動到ImageView的中央位置展示,展示圖片的中間區(qū)域。
- 當(dāng)dwidth<vwidth,dheight<vheight時,其實(shí)就是將圖片向右和向上移動到ImageView的中央位置展示。
- 當(dāng)dwidth>vwidth,dheight>vheight時,其實(shí)就是將圖片向左和向上移動到ImageView的中央位置展示
- 當(dāng)dheight>vheight,dwidth<vwidth,其實(shí)就是將圖片向右和向上移動到ImageView的中央位置展示,展示圖片中間內(nèi)容。
3、CENTER_CROP
該模式按比例擴(kuò)大圖片的尺寸并居中顯示,使得圖片長(寬)等于或大于View的長(寬)。
- 如果dwidth/dheight>vwidth/vheight(圖片的寬高比大于ImageView的寬高比),即vheight/dheight>vwidth/dwidth,也就是說ImageView和圖片高度比小于ImageView和圖片的寬度比,這時候取vheight/dheight的比例進(jìn)行圖片縮放,這樣就能保證圖片寬度在進(jìn)行同等比例縮放的時候,圖片寬度大于或等于ImageView的寬度,因?yàn)?vheight/dheight)* dwidth>vwidth。
同理,如果vwidth/dwidth>vheight/dheight時,取vwidth/dwidth作為圖片的縮放比例,可以保證縮放完成后圖片寬度等于ImageView的寬度,圖片的高度大于或等于ImageView的高度,因?yàn)?vwidth/dwidth)*dheight>vheight。圖片在縮放之后再進(jìn)行CENTER操作即可。
vwidth/dwidth>vheight/dheight,圖片先按照vwidth/dwidth進(jìn)行縮放,縮放后的圖片高度>=vheight,然后再進(jìn)行向上移位。
vheight/dheight>vwidth/dwidth,圖片先按照vheight/dheight進(jìn)行縮放,縮放后的圖片寬度>=vwidth,然后再進(jìn)行想左移位。
4、CENTER_INSIDE
將圖片的內(nèi)容完整居中顯示,通過按比例縮小或原圖片的尺寸,使得圖片長或者寬等于或小于ImageView的長或者寬,這種方式允許圖片不塞滿整個ImageView。如果圖片寬度dwidth小于ImageView的寬度vwidth,且圖片高度dheight小于ImageView高度vheight
CENTER_INSIDE和CENTER_CROP選擇縮放比例的策略正好相反,CENTER_INSIDE始終選擇較小的比例進(jìn)行縮放,保證縮放后的一個邊(寬或高)小于ImageView的寬或高。下圖所示為vheight/dheight>vwidth/dwidth的場景,取vwidth/dwidth作為圖片的縮放比例,保證了縮放后圖片高度小于或等于ImageView的高度(vwidth/dwidth)dheight<=vheight。
vwidth/dwidth>vheight/dheight的場景,取vheight/dheight作為圖片的縮放比例,保證了縮放后圖片寬度小于或等于ImageView的寬度,(vheight/dheight)dwidth<=vwidth。
5、FIT_XY、FIT_CENTER、FIT_START、FIT_END
當(dāng)ImageView設(shè)置了這四種ScaleType后,ImageView采用Matrit.ScaleToFit來進(jìn)行圖片的展示。
ScaleType.FIT_XY
FILL 圖片寬度和高度有獨(dú)立的縮放比例,保證圖片能夠塞滿整個ImageView。這種場景下圖片寬度的縮放比例為vwidth/dwidth,高度的縮放比例為vheight/dheight。
ScaleType.FIT_START
START 把圖片按比例擴(kuò)大/縮小到View的寬度或高度,顯示在View的left和top位置(圖片會完整顯示)
ScaleType.FIT_CENTER
把圖片按比例擴(kuò)大/縮小到View的寬度或高度,居中顯示(圖片會完整顯示)。
ScaleType.FIT_END
END把圖片按比例擴(kuò)大/縮小到View的寬度或高度,顯示在View的right和bottom位置(圖片會完整顯示)。
scaletype
/**
* Options for scaling the bounds of an image to the bounds of this view.
*/
public enum ScaleType {
/**
* Scale using the image matrix when drawing. The image matrix can be set using
* {@link ImageView#setImageMatrix(Matrix)}. From XML, use this syntax:
* <code>android:scaleType="matrix"</code>.
*/
MATRIX (0),
/**
* Scale the image using {@link Matrix.ScaleToFit#FILL}.
* From XML, use this syntax: <code>android:scaleType="fitXY"</code>.
*/
FIT_XY (1),
/**
* Scale the image using {@link Matrix.ScaleToFit#START}.
* From XML, use this syntax: <code>android:scaleType="fitStart"</code>.
*/
FIT_START (2),
/**
* Scale the image using {@link Matrix.ScaleToFit#CENTER}.
* From XML, use this syntax:
* <code>android:scaleType="fitCenter"</code>.
*/
FIT_CENTER (3),
/**
* Scale the image using {@link Matrix.ScaleToFit#END}.
* From XML, use this syntax: <code>android:scaleType="fitEnd"</code>.
*/
FIT_END (4),
/**
* Center the image in the view, but perform no scaling.
* From XML, use this syntax: <code>android:scaleType="center"</code>.
*/
CENTER (5),
/**
* 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.
* From XML, use this syntax: <code>android:scaleType="centerCrop"</code>.
*/
CENTER_CROP (6),
/**
* 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.
* From XML, use this syntax: <code>android:scaleType="centerInside"</code>.
*/
CENTER_INSIDE (7);
ScaleType(int ni) {
nativeInt = ni;
}
final int nativeInt;
}
configurebounds
‘’
private void configureBounds() {
if (mDrawable == null || !mHaveFrame) {
return;
}
int dwidth = mDrawableWidth;
int dheight = mDrawableHeight;
int vwidth = getWidth() - mPaddingLeft - mPaddingRight;
int vheight = getHeight() - mPaddingTop - mPaddingBottom;
boolean fits = (dwidth < 0 || vwidth == dwidth) &&
(dheight < 0 || vheight == dheight);
if (dwidth <= 0 || dheight <= 0 || ScaleType.FIT_XY == mScaleType) {
/* If the drawable has no intrinsic size, or we're told to
scaletofit, then we just fill our entire view.
*/
mDrawable.setBounds(0, 0, vwidth, vheight);
mDrawMatrix = null;
} else {
// We need to do the scaling ourself, so have the drawable
// use its native size.
mDrawable.setBounds(0, 0, dwidth, dheight);
if (ScaleType.MATRIX == mScaleType) {
// Use the specified matrix as-is.
if (mMatrix.isIdentity()) {
mDrawMatrix = null;
} else {
mDrawMatrix = mMatrix;
}
} else if (fits) {
// The bitmap fits exactly, no transform needed.
mDrawMatrix = null;
} else if (ScaleType.CENTER == mScaleType) {
// Center bitmap in view, no scaling.
mDrawMatrix = mMatrix;
mDrawMatrix.setTranslate((int) ((vwidth - dwidth) * 0.5f + 0.5f),
(int) ((vheight - dheight) * 0.5f + 0.5f));
} else if (ScaleType.CENTER_CROP == mScaleType) {
mDrawMatrix = mMatrix;
float scale;
float dx = 0, dy = 0;
if (dwidth * vheight > vwidth * dheight) {
scale = (float) vheight / (float) dheight;
dx = (vwidth - dwidth * scale) * 0.5f;
} else {
scale = (float) vwidth / (float) dwidth;
dy = (vheight - dheight * scale) * 0.5f;
}
mDrawMatrix.setScale(scale, scale);
mDrawMatrix.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
} else if (ScaleType.CENTER_INSIDE == mScaleType) {
mDrawMatrix = mMatrix;
float scale;
float dx;
float dy;
if (dwidth <= vwidth && dheight <= vheight) {
scale = 1.0f;
} else {
scale = Math.min((float) vwidth / (float) dwidth,
(float) vheight / (float) dheight);
}
dx = (int) ((vwidth - dwidth * scale) * 0.5f + 0.5f);
dy = (int) ((vheight - dheight * scale) * 0.5f + 0.5f);
mDrawMatrix.setScale(scale, scale);
mDrawMatrix.postTranslate(dx, dy);
} else {
// Generate the required transform.
mTempSrc.set(0, 0, dwidth, dheight);
mTempDst.set(0, 0, vwidth, vheight);
mDrawMatrix = mMatrix;
mDrawMatrix.setRectToRect(mTempSrc, mTempDst, scaleTypeToScaleToFit(mScaleType));
}
}
}