Skip to content

Commit 29946d2

Browse files
committed
1,完善库
1 parent 88df381 commit 29946d2

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

library/src/main/java/com/flyjingfish/shapeimageviewlib/ShapeImageView.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,15 +297,17 @@ protected void onDraw(Canvas canvas) {
297297
drawBgShape(canvas);
298298
clipPadding(canvas);
299299
mDrawPath.reset();
300+
mAttacher.updateDisplayMatrix();
301+
RectF rectF = mAttacher.getDisplayRectF();
300302
if (shapeType == ShapeType.OVAL) {
301-
mDrawPath.addOval(mDrawRectF, Path.Direction.CCW);
303+
mDrawPath.addOval(rectF, Path.Direction.CCW);
302304
} else if (shapeType == ShapeType.RECTANGLE) {
303305
float leftTopRadius = ViewUtils.getRtlValue(isRtl ? endTopRadius : startTopRadius, this.leftTopRadius);
304306
float rightTopRadius = ViewUtils.getRtlValue(isRtl ? startTopRadius : endTopRadius, this.rightTopRadius);
305307
float rightBottomRadius = ViewUtils.getRtlValue(isRtl ? startBottomRadius : endBottomRadius, this.rightBottomRadius);
306308
float leftBottomRadius = ViewUtils.getRtlValue(isRtl ? endBottomRadius : startBottomRadius, this.leftBottomRadius);
307309
float[] radius = new float[]{leftTopRadius,leftTopRadius,rightTopRadius,rightTopRadius,rightBottomRadius,rightBottomRadius,leftBottomRadius,leftBottomRadius};
308-
mDrawPath.addRoundRect(mDrawRectF,radius,Path.Direction.CCW);
310+
mDrawPath.addRoundRect(rectF,radius,Path.Direction.CCW);
309311
}
310312

311313
if (shapeType == ShapeType.OVAL || shapeType == ShapeType.RECTANGLE) {

library/src/main/java/com/flyjingfish/shapeimageviewlib/ShapeImageViewAttacher.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package com.flyjingfish.shapeimageviewlib;
22

3+
import android.graphics.Canvas;
34
import android.graphics.Matrix;
5+
import android.graphics.Rect;
6+
import android.graphics.RectF;
47
import android.graphics.drawable.Drawable;
8+
import android.graphics.drawable.PictureDrawable;
59
import android.view.View;
610
import android.widget.ImageView;
711

812

913
class ShapeImageViewAttacher implements View.OnLayoutChangeListener {
1014
private final ShapeImageView mImageView;
1115
private final Matrix mBaseMatrix = new Matrix();
16+
private final Matrix mDisplayMatrix = new Matrix();
1217
private final Matrix mDrawMatrix = new Matrix();
1318
private ShapeImageView.ShapeScaleType mScaleType;
1419
private float mAutoCropHeightWidthRatio;
@@ -131,4 +136,89 @@ public Matrix getImageMatrix() {
131136
public void setAutoCropHeightWidthRatio(float autoCropHeightWidthRatio) {
132137
this.mAutoCropHeightWidthRatio = autoCropHeightWidthRatio;
133138
}
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+
134224
}

0 commit comments

Comments
 (0)