Skip to content

Commit dbff450

Browse files
committed
1,完善AlmightyShapeImageView
1 parent 63e35ab commit dbff450

File tree

2 files changed

+120
-5
lines changed

2 files changed

+120
-5
lines changed

app/src/main/java/com/flyjingfish/shapeimageview/AlmightyImageActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
2525

2626

2727
private void setData() {
28-
MyImageLoader.getInstance().load(binding.iv1, itemData, Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL, R.mipmap.img_load_placeholder, R.mipmap.img_load_placeholder);
28+
MyImageLoader.getInstance().load(binding.iv1, itemData, R.mipmap.img_load_placeholder, R.mipmap.img_load_placeholder);
2929
MyImageLoader.getInstance().load(binding.iv2, itemData, R.mipmap.img_load_placeholder, R.mipmap.img_load_placeholder);
3030
MyImageLoader.getInstance().load(binding.iv3, itemData, R.mipmap.img_load_placeholder, R.mipmap.img_load_placeholder);
3131
MyImageLoader.getInstance().load(binding.iv4, itemData, R.mipmap.img_load_placeholder, R.mipmap.img_load_placeholder);

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

Lines changed: 119 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.content.res.TypedArray;
55
import android.graphics.Canvas;
66
import android.graphics.Color;
7+
import android.graphics.Matrix;
78
import android.graphics.Paint;
89
import android.graphics.PorterDuff;
910
import android.graphics.PorterDuffXfermode;
@@ -18,14 +19,14 @@
1819

1920
public class AlmightyShapeImageView extends AppCompatImageView {
2021
private Drawable mShapeResource;
21-
private Paint mShapePaint;
22+
private final Paint mShapePaint;
2223

2324
public AlmightyShapeImageView(@NonNull Context context) {
24-
this(context,null);
25+
this(context, null);
2526
}
2627

2728
public AlmightyShapeImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
28-
this(context, attrs,0);
29+
this(context, attrs, 0);
2930
}
3031

3132
public AlmightyShapeImageView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
@@ -60,9 +61,123 @@ private void drawShape(Canvas canvas) {
6061
int paddingRight = ViewUtils.getViewPaddingRight(this);
6162
int paddingTop = getPaddingTop();
6263
int paddingBottom = getPaddingBottom();
64+
Drawable drawable = getDrawable();
65+
if (drawable == null) {
66+
return;
67+
}
68+
69+
Matrix matrix = getImageMatrix();
70+
int left;
71+
int top;
72+
int right;
73+
int bottom;
74+
if (matrix == null) {
75+
left = paddingLeft;
76+
top = paddingTop;
77+
right = width - paddingRight;
78+
bottom = height - paddingBottom;
79+
} else {
80+
float[] matrixValues = new float[9];
81+
matrix.getValues(matrixValues);
82+
int drawableWidth = drawable.getIntrinsicWidth();
83+
int drawableHeight = drawable.getIntrinsicHeight();
84+
float drawHWScale = drawableHeight * 1f / drawableWidth;
85+
86+
int shapeResourceWidth = mShapeResource.getIntrinsicWidth();
87+
int shapeResourceHeight = mShapeResource.getIntrinsicHeight();
88+
89+
float shapeHWScale = shapeResourceHeight * 1f / shapeResourceWidth;
90+
91+
float viewHWScale = height * 1f / width;
92+
93+
float pictureWidth;
94+
float pictureHeight;
95+
float shapeWidth;
96+
float shapeHeight;
97+
98+
99+
float transX;
100+
float transY;
101+
102+
ScaleType scaleType = getScaleType();
103+
if (scaleType == ScaleType.FIT_XY) {
104+
transX = paddingLeft;
105+
transY = paddingTop;
106+
pictureWidth = width - paddingLeft - paddingRight;
107+
pictureHeight = height - paddingTop - paddingBottom;
108+
shapeWidth = pictureWidth;
109+
shapeHeight = pictureHeight;
110+
} else if (scaleType == ScaleType.CENTER_CROP) {
111+
transX = paddingLeft;
112+
transY = paddingTop;
113+
pictureWidth = width - paddingLeft - paddingRight;
114+
pictureHeight = height - paddingTop - paddingBottom;
115+
if (viewHWScale > shapeHWScale) {//按drawable宽走
116+
shapeWidth = pictureWidth;
117+
shapeHeight = shapeWidth * shapeHWScale;
118+
} else {//按drawable高走
119+
shapeHeight = pictureHeight;
120+
shapeWidth = shapeHeight / shapeHWScale;
121+
}
122+
} else if (scaleType == ScaleType.CENTER) {
123+
if (drawableWidth < width || drawableHeight < height) {
124+
if (drawableWidth < width) {
125+
pictureWidth = drawableWidth;
126+
} else {
127+
pictureWidth = width;
128+
}
129+
if (drawableHeight < height) {
130+
pictureHeight = drawableHeight;
131+
} else {
132+
pictureHeight = height;
133+
}
134+
float pictureHWScale = pictureHeight / pictureWidth;
135+
if (pictureHWScale > shapeHWScale) {//按drawable宽走
136+
shapeWidth = pictureWidth;
137+
shapeHeight = shapeWidth * shapeHWScale;
138+
} else {//按drawable高走
139+
shapeHeight = pictureHeight;
140+
shapeWidth = shapeHeight / shapeHWScale;
141+
}
142+
transX = (width - pictureWidth) / 2;
143+
transY = (height - pictureHeight) / 2;
144+
} else {
145+
transX = paddingLeft;
146+
transY = paddingTop;
147+
pictureWidth = width - paddingLeft - paddingRight;
148+
pictureHeight = height - paddingTop - paddingBottom;
149+
if (viewHWScale > shapeHWScale) {//按drawable宽走
150+
shapeWidth = pictureWidth;
151+
shapeHeight = shapeWidth * shapeHWScale;
152+
} else {//按drawable高走
153+
shapeHeight = pictureHeight;
154+
shapeWidth = shapeHeight / shapeHWScale;
155+
}
156+
}
157+
} else {
158+
transX = (int) matrixValues[2] + paddingLeft;
159+
transY = (int) matrixValues[5] + paddingTop;
160+
161+
pictureWidth = drawableWidth * matrixValues[0];
162+
pictureHeight = drawableHeight * matrixValues[4];
163+
if (drawHWScale > shapeHWScale) {//按drawable宽走
164+
shapeWidth = pictureWidth;
165+
shapeHeight = shapeWidth * shapeHWScale;
166+
} else {//按drawable高走
167+
shapeHeight = pictureHeight;
168+
shapeWidth = shapeHeight / shapeHWScale;
169+
}
170+
}
171+
172+
left = ((int) (transX + (pictureWidth - shapeWidth) / 2));
173+
top = ((int) (transY + (pictureHeight - shapeHeight) / 2));
174+
right = ((int) (shapeWidth + transX + (pictureWidth - shapeWidth) / 2));
175+
bottom = ((int) (shapeHeight + transY + (pictureHeight - shapeHeight) / 2));
176+
}
177+
63178
mShapePaint.setXfermode(null);
64179
canvas.saveLayer(new RectF(0, 0, canvas.getWidth(), canvas.getHeight()), mShapePaint, Canvas.ALL_SAVE_FLAG);
65-
mShapeResource.setBounds(paddingLeft,paddingTop,width-paddingRight,height-paddingBottom);
180+
mShapeResource.setBounds(left, top, right, bottom);
66181
mShapeResource.draw(canvas);
67182
mShapePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
68183
canvas.saveLayer(new RectF(0, 0, canvas.getWidth(), canvas.getHeight()), mShapePaint, Canvas.ALL_SAVE_FLAG);

0 commit comments

Comments
 (0)