4848import android .view .View ;
4949import android .view .ViewOutlineProvider ;
5050import com .google .android .material .resources .MaterialResources ;
51+ import com .google .android .material .shape .MaterialShapeDrawable ;
5152import com .google .android .material .shape .ShapeAppearanceModel ;
5253import com .google .android .material .shape .ShapeAppearancePathProvider ;
5354import com .google .android .material .shape .Shapeable ;
@@ -68,6 +69,7 @@ public class ShapeableImageView extends AppCompatImageView implements Shapeable
6869 private ShapeAppearanceModel shapeAppearanceModel ;
6970 @ Dimension private float strokeWidth ;
7071 private Path maskPath ;
72+ private final MaterialShapeDrawable shadowDrawable ;
7173
7274 public ShapeableImageView (Context context ) {
7375 this (context , null , 0 );
@@ -104,6 +106,7 @@ public ShapeableImageView(Context context, @Nullable AttributeSet attrs, int def
104106 borderPaint .setAntiAlias (true );
105107 shapeAppearanceModel =
106108 ShapeAppearanceModel .builder (context , attrs , defStyle , DEF_STYLE_RES ).build ();
109+ shadowDrawable = new MaterialShapeDrawable (shapeAppearanceModel );
107110 if (VERSION .SDK_INT >= VERSION_CODES .LOLLIPOP ) {
108111 setOutlineProvider (new OutlineProvider ());
109112 }
@@ -137,6 +140,7 @@ protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight)
137140 @ Override
138141 public void setShapeAppearanceModel (@ NonNull ShapeAppearanceModel shapeAppearanceModel ) {
139142 this .shapeAppearanceModel = shapeAppearanceModel ;
143+ shadowDrawable .setShapeAppearanceModel (shapeAppearanceModel );
140144 updateShapeMask (getWidth (), getHeight ());
141145 invalidate ();
142146 }
@@ -149,10 +153,7 @@ public ShapeAppearanceModel getShapeAppearanceModel() {
149153
150154 private void updateShapeMask (int width , int height ) {
151155 destination .set (
152- getPaddingLeft (),
153- getPaddingTop (),
154- width - getPaddingRight (),
155- height - getPaddingBottom ());
156+ getPaddingLeft (), getPaddingTop (), width - getPaddingRight (), height - getPaddingBottom ());
156157 pathProvider .calculatePath (shapeAppearanceModel , 1f /*interpolation*/ , destination , path );
157158 // Remove path from rect to draw with clear paint.
158159 maskPath .rewind ();
@@ -252,16 +253,17 @@ public void setStrokeColor(@Nullable ColorStateList strokeColor) {
252253 @ TargetApi (VERSION_CODES .LOLLIPOP )
253254 class OutlineProvider extends ViewOutlineProvider {
254255
255- private Rect rect = new Rect ();
256+ private final Rect rect = new Rect ();
256257
257258 @ Override
258259 public void getOutline (View view , Outline outline ) {
259- if (shapeAppearanceModel != null && shapeAppearanceModel .isRoundRect (destination )) {
260- destination .round (rect );
261- float cornerSize =
262- shapeAppearanceModel .getBottomLeftCornerSize ().getCornerSize (destination );
263- outline .setRoundRect (rect , cornerSize );
260+ if (shapeAppearanceModel == null ) {
261+ return ;
264262 }
263+
264+ destination .round (rect );
265+ shadowDrawable .setBounds (rect );
266+ shadowDrawable .getOutline (outline );
265267 }
266268 }
267269}
0 commit comments