3

I want to draw rectangle at center of a view like this image .For this I am using the following code

public class TransparentView extends View {


    public TransparentView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

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

    public TransparentView(Context context) {
        super(context);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        canvas.drawColor(Color.parseColor("#60000000"));
        Paint borderPaint = new Paint();
        borderPaint.setARGB(255, 255, 128, 0);
        borderPaint.setStyle(Paint.Style.STROKE);
        borderPaint.setStrokeWidth(4);

        Paint innerPaint = new Paint();
        innerPaint.setARGB(0, 0, 0, 0);
        innerPaint.setAlpha(0);
        innerPaint.setStyle(Paint.Style.FILL);

        RectF drawRect = new RectF();

        drawRect.set(100, 100, 100,100);

        canvas.drawRect(drawRect, innerPaint);
        canvas.drawRect(drawRect, borderPaint);

    }

}

How can i make complete transparent inside the rectangle and poor transparent at the outer region?

Community
  • 1
  • 1
Asha Soman
  • 199
  • 2
  • 3
  • 11

4 Answers4

12

Just use

innerPaint.setStyle(Paint.Style.STROKE);

instead of

innerPaint.setStyle(Paint.Style.FILL);
Leos Literak
  • 8,805
  • 19
  • 81
  • 156
naran zala
  • 141
  • 1
  • 6
2

Use Path and add two Rects into it, one for the full View area and one for the “selection”. Set Path fillType to EVEN_ODD. Now fill that with half-transparent color.

Path outerPath = new Path();
// add rect covering the whole view area
outerPath.addRect(0, 0, getWidth(), getHeight(), Path.Direction.CW);
// add "selection" rect;
RectF inner = new RectF(left, top, right, bottom);
outerPath.addRect(inner, Path.Direction.CW);
// set the fill rule so inner area will not be painted
outerPath.setFillType(Path.FillType.EVEN_ODD);

// set up paints
Paint outerPaint = new Paint();
outerPaint.setColor(0x60000000);

Paint borderPaint = new Paint();
borderPaint.setARGB(255, 255, 128, 0);
borderPaint.setStyle(Paint.Style.STROKE);
borderPaint.setStrokeWidth(4);

// draw
canvas.drawPath(outerPath, outerPaint);
canvas.drawRect(inner, borderPaint);

Note that for better performance you should not create Paints and Paths in onPaint(). I suggest onSizeChanged() is better for that.

geeksunny
  • 83
  • 2
  • 5
user1410657
  • 1,284
  • 9
  • 5
1

I separated the image in 5 rectangles. The main one is the complete transparent one, that's in the middle of the figure. The other 4 will be semi-transparent.

After that, inside onDraw(Canvas canvas) I painted over the 4 rectangles with a semi-transparent color.

Here's the code I used.

//On constructor:
float x1,x2,y1,y2;
x1 = (screenWidth - squareSize)/2;
x2 = x1 + squareSize;
y1 = (screenHeight - squareSize)/2;
y2 = y1 + squareSize;
rect = new RectF(x1, y1, x2, y2);
dark = new RectF[]{
        new RectF(0, 0, screenWidth, y1),
        new RectF(0, y1, x1, y2),
        new RectF(x2, y1, screenWidth, y2),
        new RectF(0, y2, screenWidth, screenHeight)
};

semitransparentPaint = new Paint();
semitransparentPaint.setColor(Color.BLACK);
semitransparentPaint.setAlpha((int) (256 * (1-TRANSPARENCY)));



//Inside onDraw(Canvas canvas)
for(RectF r : dark){
    canvas.drawRect(r, semitransparentPaint);
}

That may work for you. Also, you didn't ask for the borders, but on your code snippet I saw that you called canvas.drawRect(drawRect, borderPaint); witch makes me believe you also want to draw them, so I'll paste here the code I used to draw the borders:

//Inside onDraw(Canvas canvas)
canvas.drawLines(new float[]{
    rect.left, rect.top, rect.right, rect.top, 
    rect.right, rect.top, rect.right, rect.bottom,
    rect.right, rect.bottom, rect.left, rect.bottom,
    rect.left, rect.bottom, rect.left, rect.top}
    , 0, 16, squareBorderPaint);
Leandroid
  • 1,997
  • 2
  • 17
  • 19
0

You can make use of setXfermode() of Paint and pass PorterDuff.Mode.ADD and PorterDuff.Mode.ADD as parameter in order to obtain inner transparency region.

I have mentioned an example below which contains a transparent rectangle inside another rectangle, which show's how it can be implemented in Android.

    Paint outerPaint = new Paint();
    outerPaint.setColor(0x7f0600a6); // mention any background color 
    outerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));
    outerPaint.setAntiAlias(true);

    Paint innerPaint = new Paint();
    innerPaint.setColor(Color.TRANSPARENT);
    innerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    innerPaint.setAntiAlias(true);

    canvas.drawRect(0.0F, 0.0F, width, height, outerPaint);
    canvas.drawRect(150F, 150F, innerWidth, innerHeight, innerPaint);

Please Note: You may change the values within drawRect() as per your needs.

Arshak
  • 3,197
  • 1
  • 25
  • 33