AshMv
AshMv

Reputation: 392

Drawing double colored square on Android canvas transparent rectangle

I have several lines in my application. If somebody touches a line I have to highlight the line touched. I think if I could draw a transparent rectangle with light color, other than the clicked line color, then it will be highlighted properly.. So can anybody tell me how can I draw a transparent rectangle on Android canvas? My line color is black. Please see the pic.

enter image description here

Upvotes: 1

Views: 788

Answers (3)

Piyush
Piyush

Reputation: 18933

you can use this:

Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.MAGENTA);
DashPathEffect dashPath =new DashPathEffect(new float[ ]{70,2}, 30);
paint.setPathEffect(dashPath);
paint.setStrokeWidth(80);
canvas.drawRect(30, 300 , 320, 300, paint);

Upvotes: 1

matejs
matejs

Reputation: 3536

This will draw green 50% transparent rectangle on canvas:

Paint myPaint = new Paint();
myPaint.setStyle(Paint.Style.FILL);
myPaint.setColor(Color.rgba(0, 256, 0, 128)); // green color with 50% transparency
// c is your canvas
c.drawRect(100, 100, 200, 200, myPaint);

Upvotes: 2

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

try this way

private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(3);
        mPaint.setPathEffect(null);
        canvas.drawRect(x, y, x + width, y + height, mPaint);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(3);
        mPaint.setPathEffect(new DashPathEffect(new float[] { 5, 5 }, 5));
        canvas.drawRect(x, y, x + width, y + height, mPaint);

Upvotes: 1

Related Questions