Skip to content

Commit

Permalink
[ShapeableImageView] Fixed shadows for non round rects
Browse files Browse the repository at this point in the history
#1562

PiperOrigin-RevId: 325069672
(cherry picked from commit fbefa5a)
  • Loading branch information
ymarian authored and dsn5ft committed Aug 28, 2020
1 parent c9e2ba0 commit 821bf26
Showing 1 changed file with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import android.view.View;
import android.view.ViewOutlineProvider;
import com.google.android.material.resources.MaterialResources;
import com.google.android.material.shape.MaterialShapeDrawable;
import com.google.android.material.shape.ShapeAppearanceModel;
import com.google.android.material.shape.ShapeAppearancePathProvider;
import com.google.android.material.shape.Shapeable;
Expand All @@ -68,6 +69,7 @@ public class ShapeableImageView extends AppCompatImageView implements Shapeable
private ShapeAppearanceModel shapeAppearanceModel;
@Dimension private float strokeWidth;
private Path maskPath;
private final MaterialShapeDrawable shadowDrawable;

public ShapeableImageView(Context context) {
this(context, null, 0);
Expand Down Expand Up @@ -104,6 +106,7 @@ public ShapeableImageView(Context context, @Nullable AttributeSet attrs, int def
borderPaint.setAntiAlias(true);
shapeAppearanceModel =
ShapeAppearanceModel.builder(context, attrs, defStyle, DEF_STYLE_RES).build();
shadowDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
setOutlineProvider(new OutlineProvider());
}
Expand Down Expand Up @@ -137,6 +140,7 @@ protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight)
@Override
public void setShapeAppearanceModel(@NonNull ShapeAppearanceModel shapeAppearanceModel) {
this.shapeAppearanceModel = shapeAppearanceModel;
shadowDrawable.setShapeAppearanceModel(shapeAppearanceModel);
updateShapeMask(getWidth(), getHeight());
invalidate();
}
Expand All @@ -149,10 +153,7 @@ public ShapeAppearanceModel getShapeAppearanceModel() {

private void updateShapeMask(int width, int height) {
destination.set(
getPaddingLeft(),
getPaddingTop(),
width - getPaddingRight(),
height - getPaddingBottom());
getPaddingLeft(), getPaddingTop(), width - getPaddingRight(), height - getPaddingBottom());
pathProvider.calculatePath(shapeAppearanceModel, 1f /*interpolation*/, destination, path);
// Remove path from rect to draw with clear paint.
maskPath.rewind();
Expand Down Expand Up @@ -252,16 +253,17 @@ public void setStrokeColor(@Nullable ColorStateList strokeColor) {
@TargetApi(VERSION_CODES.LOLLIPOP)
class OutlineProvider extends ViewOutlineProvider {

private Rect rect = new Rect();
private final Rect rect = new Rect();

@Override
public void getOutline(View view, Outline outline) {
if (shapeAppearanceModel != null && shapeAppearanceModel.isRoundRect(destination)) {
destination.round(rect);
float cornerSize =
shapeAppearanceModel.getBottomLeftCornerSize().getCornerSize(destination);
outline.setRoundRect(rect, cornerSize);
if (shapeAppearanceModel == null) {
return;
}

destination.round(rect);
shadowDrawable.setBounds(rect);
shadowDrawable.getOutline(outline);
}
}
}

0 comments on commit 821bf26

Please sign in to comment.