I have created a class for a rectangle shape as it is an object in my application. However now I would like the corners to be rounded. Below you can see it's a bare bones class to create as many rectangles I want with the same attributes.
public class customDrawable extends ShapeDrawable {
public void setCustomDrawableAttributes(ShapeDrawable shapeDrawable){
int x = 0;
int y = 0;
int width = 100;
int height = 100;
shapeDrawable.setBounds(x, y-height, x + width,y+height );
}
public ShapeDrawable createShape(){
return new ShapeDrawable(new RectShape());
}
}
Update: Without this method I have nothing will be drawn as there is no size. With it it only draws the usual rectangle. (Integer values where changed to not show app specific methods)
public void setDrawableAttributes(ShapeDrawable shapeDrawable){
int x = 0;
int y = 500
int width = 200
int height = 300
shapeDrawable.setBounds(x, y-height, x + width,y+height );
}
From my research I found that I cannot simply add rounded corners but instead have to create a RoundRectShape shapeDrawable
. Every attempt I have made to create a rectangle with rounded corners using this RoundRectShape
has failed. Somehow the shape always ends up being a regular rectangle with no rounded corners.
I am looking for a just bare bones class (like the one provided) that creates a roundRectShape drawable. Height and width does not matter as long as it has rounded corners. Must be in Java and not XML.
Links I have tried for creating round rectangles:
1.https://developer.android.com/reference/android/graphics/drawable/shapes/RoundRectShape.html
6.Android: RoundRectShape: Modify corner radii
8.http://www.edumobile.org/android/shape-drawing-example-in-android/ 9.http://programtalk.com/java-api-usage-examples/android.graphics.drawable.shapes.RoundRectShape/
I've created a class
MyRect
that is used to draw rounded Rect for you.Creating objects of above
MyRect
is not enough, we need to keep references of objects in any container so that we can modify or delete that object in future.Inside
onDraw(Canvas canvas)
method ofView
/SurfaceView
call MyRect'sdraw()
method.Done, Create object and add to container.
or
You can also extends
MyRect
like add some more constructor, method and data member according to your requirement.