|
4 | 4 | import android.content.res.TypedArray; |
5 | 5 | import android.graphics.Canvas; |
6 | 6 | import android.graphics.Color; |
| 7 | +import android.graphics.Matrix; |
7 | 8 | import android.graphics.Paint; |
8 | 9 | import android.graphics.PorterDuff; |
9 | 10 | import android.graphics.PorterDuffXfermode; |
|
18 | 19 |
|
19 | 20 | public class AlmightyShapeImageView extends AppCompatImageView { |
20 | 21 | private Drawable mShapeResource; |
21 | | - private Paint mShapePaint; |
| 22 | + private final Paint mShapePaint; |
22 | 23 |
|
23 | 24 | public AlmightyShapeImageView(@NonNull Context context) { |
24 | | - this(context,null); |
| 25 | + this(context, null); |
25 | 26 | } |
26 | 27 |
|
27 | 28 | public AlmightyShapeImageView(@NonNull Context context, @Nullable AttributeSet attrs) { |
28 | | - this(context, attrs,0); |
| 29 | + this(context, attrs, 0); |
29 | 30 | } |
30 | 31 |
|
31 | 32 | public AlmightyShapeImageView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { |
@@ -60,9 +61,123 @@ private void drawShape(Canvas canvas) { |
60 | 61 | int paddingRight = ViewUtils.getViewPaddingRight(this); |
61 | 62 | int paddingTop = getPaddingTop(); |
62 | 63 | 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 | + |
63 | 178 | mShapePaint.setXfermode(null); |
64 | 179 | 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); |
66 | 181 | mShapeResource.draw(canvas); |
67 | 182 | mShapePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); |
68 | 183 | canvas.saveLayer(new RectF(0, 0, canvas.getWidth(), canvas.getHeight()), mShapePaint, Canvas.ALL_SAVE_FLAG); |
|
0 commit comments