I need to make a rectangle which has a circle hole in middle something like this
Asked
Active
Viewed 428 times
-1

Gabriele Mariotti
- 320,139
- 94
- 887
- 841

amirhesni
- 441
- 1
- 6
- 22
-
use `ShapeDrawable` with a custom `Shape` - when implementing `Shape.draw()` method use `Canvas.drawPath()` – pskink Jan 15 '20 at 09:09
-
I do not know how to work with that can you show me complete code – amirhesni Jan 15 '20 at 09:16
-
what is in MyShape class ? – amirhesni Jan 15 '20 at 09:54
-
your shape you want to draw - it is done inside `draw()` method – pskink Jan 15 '20 at 09:55
-
I don't know how to draw my shape that's the question ;) – amirhesni Jan 15 '20 at 09:57
-
did you read `Path` class documentation? basically you need to use `Path` methods to form the `Path` and draw it using `Canvas.drawPath()` method – pskink Jan 15 '20 at 09:58
-
1No I haven't ... – amirhesni Jan 15 '20 at 09:59
-
Check out the Porterduff mode. And see how to combine (or cut out) a mask from a bitmap. https://developer.android.com/reference/android/graphics/PorterDuff.Mode – Phantômaxx Jan 15 '20 at 10:12
-
what do you want it for? beside using Path and Canvas, I think an easy but not a very good way is to use an image view, make the image with photoshop or sth and use it in your project – Mohsen Jan 15 '20 at 10:15
-
@mohsen I was thinking that but I hoped there is better solution than that – amirhesni Jan 15 '20 at 10:19
-
there is but it's much harder :D – Mohsen Jan 15 '20 at 10:28
-
1Take a look at the MaterialShapeDrawable in the MaterialComponents Library. Just apply the BottomAppBarTopEdgeTreatment on the bottom edge (or take it as example) to obtain your shape. – Gabriele Mariotti Jan 15 '20 at 10:33
1 Answers
3
There are different ways to obtain it.
You can use the Material Components Library and the MaterialShapeDrawable
to create custom shape path (you need the version 1.1.0
)
For example you can do something like:
LinearLayout linearLayout= findViewById(R.id.linear_rounded);
//Use the BottomAppBarTopEdgeTreatment to apply the bottom edge shape, or just create a custom class to obtain a similar shape
BottomAppBarTopEdgeTreatment bottomAppBarTopEdgeTreatment = new BottomAppBarTopEdgeTreatment(
getResources().getDimension(R.dimen.margin),
getResources().getDimension(R.dimen.rounded_corner),
getResources().getDimension(R.dimen.vertical_offset)
);
bottomAppBarTopEdgeTreatment.setFabDiameter(getResources().getDimension(R.dimen.diameter));
ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
.toBuilder()
.setAllCorners(CornerFamily.ROUNDED,radius)
.setBottomLeftCorner(CornerFamily.ROUNDED,0)
.setBottomRightCorner(CornerFamily.ROUNDED,0)
.setBottomEdge(bottomAppBarTopEdgeTreatment)
.build();
MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
ViewCompat.setBackground(linearLayout,shapeDrawable);

Gabriele Mariotti
- 320,139
- 94
- 887
- 841
-
that sounds perfect but i couldn't find MaterialShapeDrawable dependency can you add it too ? – amirhesni Jan 15 '20 at 11:27
-
@amirhesni You can find all [info here](https://github.com/material-components/material-components-android/blob/master/docs/getting-started.md). Use the version 1.1.0-rc01 – Gabriele Mariotti Jan 15 '20 at 13:28