0

rectangles

I already drew the left rounded rectangle. I need to insert some lines horizontally for it to become a striped rectangle. How can I achieve this?

I already tried to use path and draw a line. But nothing worked how it was supposed to. Can someone help me?

1 Answers1

0

Here's a sample code for drawing a striped rectangle in Android Canvas:

public class StripeRectView extends View {

    private Paint mPaint;
    private int mStripeWidth = 10;
    private int mStripeHeight = 10;

    public StripeRectView(Context context) {
        super(context);
        init();
    }

    public StripeRectView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public StripeRectView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int x = 0;
        int y = 0;
        while (y <= getHeight()) {
            canvas.drawRect(x, y, x + mStripeWidth, y + mStripeHeight, mPaint);
            x += mStripeWidth * 2;
            if (x > getWidth()) {
                x = 0;
                y += mStripeHeight * 2;
            }
        }
    }
}

the Canvas is used to draw rectangles, with alternating stripes of width mStripeWidth and height mStripeHeight until the entire height of the View is filled. You can modify it according to your needs.

Aniket kumar
  • 212
  • 1
  • 4