Created
March 19, 2014 06:40
-
-
Save shaobin0604/9636554 to your computer and use it in GitHub Desktop.
ImageView ScaleType
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ImageView { | |
| 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)); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment