Android Canvas: Create RoundRectShape Object

4.4k Views Asked by At

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

2.http://alvinalexander.com/java/jwarehouse/android/graphics/java/android/graphics/drawable/shapes/RoundRectShape.java.shtml

3.https://www.codota.com/android/scenarios/52c5d269da0a37e1836d6e75/android.graphics.drawable.shapes.RoundRectShape?tag=coyote

4.http://developer.oesf.biz/em/developer/reference/durian/android/graphics/drawable/shapes/RoundRectShape.html

5.https://android.googlesource.com/platform/frameworks/base/+/donut-release/graphics/java/android/graphics/drawable/shapes/RoundRectShape.java

6.Android: RoundRectShape: Modify corner radii

7.http://www.programcreek.com/java-api-examples/index.php?api=android.graphics.drawable.shapes.RoundRectShape

8.http://www.edumobile.org/android/shape-drawing-example-in-android/ 9.http://programtalk.com/java-api-usage-examples/android.graphics.drawable.shapes.RoundRectShape/

3

There are 3 best solutions below

0
On BEST ANSWER

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 MyRectlike add some more constructor, method and data member according to your requirement.

6
On

Why don't you use the drawRoundRect function of the Canvas class? public class RoundRect{ int l,r,t,b,rx,ry; Paint paint;

    public RoundRect(int l,int r,int t,int b,int rx,int ry,Paint paint){
        this.l=l;
        this.r=r;
        this.t=t;
        this.b=b;
        this.paint=paint;
    }
    public void draw(Canvas c,Paint paint){ 
        c.drawRoundRect(l,t,r,b,rx,ry,paint);
    }
}`
1
On

Custom Drawable

You can create custom drawable by extending drawable class

Steps to create Custom Drawable

1.Subclass Drawable and implement the following methods methods

  • public void draw(@NonNull Canvas canvas) - You will get a canvas object to draw shapes.Call getBounds() method here to get the dimensions according view in which we applied the drawable.
  • public void setAlpha(@IntRange(from = 0, to = 255) int alpha) - You will get a alpha integer value here,set it your main paint in which you are drawing the shape.
  • public void setColorFilter(@Nullable ColorFilter colorFilter) - You will get ColorFilter object here,set it your main paint in which you are drawing the shape.
  • public int getOpacity() - Return the opacity value here like PixelFormat.TRANSLUCENT,PixelFormat.TRANSPARENT,PixelFormat.RGB_888 etc.

2.In onDraw() call canvas.drawRoundRect() method to draw the shape

  • public void drawRoundRect(@android.support.annotation.NonNull android.graphics.RectF rect,float rx,float ry,@android.support.annotation.NonNull android.graphics.Paint paint)

    Draw the specified round-rect using the specified paint. The roundrect will be filled or framed based on the Style in the paint.

    Parameters:

    1) rect - The rectangular bounds of the roundRect to be drawn 2) rx - The x-radius of the oval used to round the corners 3) ry - The y-radius of the oval used to round the corners 4) paint - The paint used to draw the roundRect

Code Sample

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

/**
 * Created by jinesh on 24/5/17.
 */

public class RoundedRectangle extends Drawable {
    private Paint rectPaint;
    private RectF drawableBounds;
    public RoundedRectangle(int rectBackground) {
        rectPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        rectPaint.setColor(rectBackground);
        drawableBounds=new RectF();
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        Rect bounds=getBounds();
        drawableBounds.set(bounds.left,bounds.top,bounds.right,bounds.bottom);
        canvas.drawRoundRect(drawableBounds,10,10,rectPaint);
    }

    @Override
    public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {
       rectPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
       rectPaint.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
}

Set to any view from activity

 RoundedRectangle roundedRectangle=new RoundedRectangle(ContextCompat.getColor(this,R.color.colorAccent));
 textView.setBackground(roundedRectangle);

Screenshot:

enter image description here