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.
public class MyRect {
public static Paint paint; // default paint use for all my MyRect objects
static {
paint=new Paint();
paint.setColor(Color.RED);
}
public float x,y,width,height;
public float roundStrength=30;
public MyRect(float x, float y, float width,float height){
this.x=x;
this.y=y;
this.width=width;
this.height=height;
}
public MyRect(float x, float y, float width,float height,float roundStrength){
this(x,y,width,height);
this.roundStrength=roundStrength;
}
public void draw(Canvas canvas){
canvas.drawRoundRect(x-width/2,y-height/2,x+width/2,y+height/2,roundStrength,roundStrength,paint);
}
}
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.
static ArrayList<MyRect> myRects=new ArrayList<>();
Inside onDraw(Canvas canvas)
method of View
/SurfaceView
call MyRect's draw()
method.
for(MyRect rect:myRects)
rect.draw(canvas);
Done, Create object and add to container.
myRects.add(new MyRect(touchx,touchy,100,100));
or
myRects.add(new MyRect(touchx,touchy,100,100,50));
You can also extends MyRect
like add some more constructor, method and data member according to your requirement.