|
1 | 1 | package com.flyjingfish.shapeimageviewlib; |
2 | 2 |
|
| 3 | +import android.graphics.Canvas; |
3 | 4 | import android.graphics.Matrix; |
| 5 | +import android.graphics.Rect; |
| 6 | +import android.graphics.RectF; |
4 | 7 | import android.graphics.drawable.Drawable; |
| 8 | +import android.graphics.drawable.PictureDrawable; |
5 | 9 | import android.view.View; |
6 | 10 | import android.widget.ImageView; |
7 | 11 |
|
8 | 12 |
|
9 | 13 | class ShapeImageViewAttacher implements View.OnLayoutChangeListener { |
10 | 14 | private final ShapeImageView mImageView; |
11 | 15 | private final Matrix mBaseMatrix = new Matrix(); |
| 16 | + private final Matrix mDisplayMatrix = new Matrix(); |
12 | 17 | private final Matrix mDrawMatrix = new Matrix(); |
13 | 18 | private ShapeImageView.ShapeScaleType mScaleType; |
14 | 19 | private float mAutoCropHeightWidthRatio; |
@@ -131,4 +136,89 @@ public Matrix getImageMatrix() { |
131 | 136 | public void setAutoCropHeightWidthRatio(float autoCropHeightWidthRatio) { |
132 | 137 | this.mAutoCropHeightWidthRatio = autoCropHeightWidthRatio; |
133 | 138 | } |
| 139 | + |
| 140 | + public void updateDisplayMatrix() { |
| 141 | + Drawable drawable = mImageView.getDrawable(); |
| 142 | + if (drawable == null) { |
| 143 | + return; |
| 144 | + } |
| 145 | + final float viewWidth = getImageViewWidth(mImageView); |
| 146 | + final float viewHeight = getImageViewHeight(mImageView); |
| 147 | + final int drawableWidth = drawable.getIntrinsicWidth(); |
| 148 | + final int drawableHeight = drawable.getIntrinsicHeight(); |
| 149 | + mDisplayMatrix.reset(); |
| 150 | + final float widthScale = viewWidth / drawableWidth; |
| 151 | + final float heightScale = viewHeight / drawableHeight; |
| 152 | + if (mScaleType == ShapeImageView.ShapeScaleType.CENTER) { |
| 153 | + mDisplayMatrix.postTranslate((viewWidth - drawableWidth) / 2F, |
| 154 | + (viewHeight - drawableHeight) / 2F); |
| 155 | + |
| 156 | + } else if (mScaleType == ShapeImageView.ShapeScaleType.CENTER_CROP) { |
| 157 | + float scale = Math.max(widthScale, heightScale); |
| 158 | + mDisplayMatrix.postScale(scale, scale); |
| 159 | + mDisplayMatrix.postTranslate((viewWidth - drawableWidth * scale) / 2F, |
| 160 | + (viewHeight - drawableHeight * scale) / 2F); |
| 161 | + |
| 162 | + } else if (mScaleType == ShapeImageView.ShapeScaleType.CENTER_INSIDE) { |
| 163 | + float scale = Math.min(1.0f, Math.min(widthScale, heightScale)); |
| 164 | + mDisplayMatrix.postScale(scale, scale); |
| 165 | + mDisplayMatrix.postTranslate((viewWidth - drawableWidth * scale) / 2F, |
| 166 | + (viewHeight - drawableHeight * scale) / 2F); |
| 167 | + |
| 168 | + } else if (this.mScaleType == ShapeImageView.ShapeScaleType.START_CROP|| |
| 169 | + this.mScaleType == ShapeImageView.ShapeScaleType.END_CROP|| |
| 170 | + this.mScaleType == ShapeImageView.ShapeScaleType.AUTO_START_CENTER_CROP|| |
| 171 | + this.mScaleType == ShapeImageView.ShapeScaleType.AUTO_END_CENTER_CROP) { |
| 172 | + float scale = Math.max(widthScale, heightScale); |
| 173 | + mDisplayMatrix.postScale(scale, scale); |
| 174 | + mDisplayMatrix.postTranslate((viewWidth - drawableWidth * scale) / 2F, |
| 175 | + (viewHeight - drawableHeight * scale) / 2F); |
| 176 | + } else { |
| 177 | + RectF mTempSrc = new RectF(0, 0, drawableWidth, drawableHeight); |
| 178 | + RectF mTempDst = new RectF(0, 0, viewWidth, viewHeight); |
| 179 | + switch (mScaleType) { |
| 180 | + case FIT_CENTER: |
| 181 | + mDisplayMatrix.setRectToRect(mTempSrc, mTempDst, Matrix.ScaleToFit.CENTER); |
| 182 | + break; |
| 183 | + case FIT_START: |
| 184 | + mDisplayMatrix.setRectToRect(mTempSrc, mTempDst, Matrix.ScaleToFit.START); |
| 185 | + break; |
| 186 | + case FIT_END: |
| 187 | + mDisplayMatrix.setRectToRect(mTempSrc, mTempDst, Matrix.ScaleToFit.END); |
| 188 | + break; |
| 189 | + case FIT_XY: |
| 190 | + mDisplayMatrix.setRectToRect(mTempSrc, mTempDst, Matrix.ScaleToFit.FILL); |
| 191 | + break; |
| 192 | + default: |
| 193 | + break; |
| 194 | + } |
| 195 | + } |
| 196 | + getDisplayRect(mDisplayMatrix); |
| 197 | + } |
| 198 | + |
| 199 | + private final RectF mDisplayRectF = new RectF(); |
| 200 | + |
| 201 | + private void getDisplayRect(Matrix matrix) { |
| 202 | + Drawable drawable = mImageView.getDrawable(); |
| 203 | + if (drawable == null) { |
| 204 | + return; |
| 205 | + } |
| 206 | + mDisplayRectF.set(0, 0, drawable.getIntrinsicWidth(), |
| 207 | + drawable.getIntrinsicHeight()); |
| 208 | + matrix.mapRect(mDisplayRectF); |
| 209 | + final float viewWidth = getImageViewWidth(mImageView); |
| 210 | + final float viewHeight = getImageViewHeight(mImageView); |
| 211 | + float paddingLeft = ViewUtils.getViewPaddingLeft(mImageView); |
| 212 | + float paddingTop = mImageView.getPaddingTop(); |
| 213 | + float left = Math.max(mDisplayRectF.left,0)+paddingLeft; |
| 214 | + float top = Math.max(mDisplayRectF.top,0)+paddingTop; |
| 215 | + float right = Math.min(mDisplayRectF.right,viewWidth)+paddingLeft; |
| 216 | + float bottom = Math.min(mDisplayRectF.bottom,viewHeight)+paddingTop; |
| 217 | + mDisplayRectF.set((int) left, (int) top, (int) right, (int) bottom); |
| 218 | + } |
| 219 | + |
| 220 | + public RectF getDisplayRectF() { |
| 221 | + return mDisplayRectF; |
| 222 | + } |
| 223 | + |
134 | 224 | } |
0 commit comments