canvas.drawRect doesn't draw a rectangle on the image


Krishna

Here canvas.drawRect will be different when using different bitmaps. I want to draw a rectangle over the top image and want the part of the image outside the rectangle to be darkened or blurred. please help me.

draw.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Paint paint = new Paint();
            paint.setColor(Color.RED);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(2);

            Bitmap bitmap=((BitmapDrawable)image.getDrawable()).getBitmap();
            Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.RGB_565, true);

            Bitmap bitmap1 = Bitmap.createBitmap(600,400, Bitmap.Config.RGB_565);

            Rect r = new Rect();
            r.set(100,100,500,300);

            Canvas canvas = new Canvas(mutableBitmap);
            Canvas canvas1 = new Canvas(bitmap1);

            canvas.drawRect(r,paint);
            canvas1.drawRect(r,paint);

            image.setImageBitmap(mutableBitmap);
            image1.setImageBitmap(bitmap1);

        }
 });

enter image description here

Venkata

If you want to draw a rectangle and darken the outside, do it this way. Create a bitmap and draw a full-sized semi-transparent rectangle, and then draw another rectangle with a full-transparent color inside it. Then put that bitmap on the image.

Try the following code:

            Bitmap bitmap = Bitmap.createBitmap(600,400,Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);

            RectF outerRectangle = new RectF(0, 0, 600, 400);
            RectF innerRectangle = new RectF(100, 100, 500, 300);

            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(Color.BLACK);
            paint.setAlpha(80);
            canvas.drawRect(outerRectangle, paint);

            paint.setColor(Color.TRANSPARENT);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
            canvas.drawRect(innerRectangle, paint);

            paint.setColor(Color.RED);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(2);
            canvas.drawRect(innerRectangle, paint);

            image1.setImageBitmap(bitmap);

Related


canvas.drawRect doesn't draw a rectangle on the image

Krishna Here canvas.drawRect will be different when using different bitmaps. I want to draw a rectangle over the top image and want the part of the image outside the rectangle to be darkened or blurred. Please help me. draw.setOnClickListener(new View.OnClickL

Canvas.drawRect() doesn't draw anything

Cody My painter in the Canvas class doesn't draw the rectangle, although I configure it to do so. I have a simple canvas layout, I have set up 6 rectangle painters that should draw a grey rectangle if the corresponding boolean flag is true. Still, for some rea

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

React-Native-Canvas doesn't draw whole rectangle

Brian Ton While making another app, I ran into a problem react-native-canvas, so I created this minimal project. The problem is that the WebView is much smaller than the Canvas and doesn't fill the entire canvas when appropriate. Here is the code (very similar

React-Native-Canvas doesn't draw whole rectangle

Brian Ton While making another app, I ran into a problem react-native-canvas, so I created this minimal project. The problem is that the WebView is much smaller than the Canvas and doesn't fill the entire canvas when appropriate. Here is the code (very similar

android drawRect doesn't draw anything

Domic I ran into this basic drawRect and nothing was displayed and I don't know why. onDraw protected void onDraw(Canvas canvas) { canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint); canvas.drawCircle(circle1x, circle1y, circleRadius, circlePaint);

Can't Graphics.drawRect fully draw a rectangle?

mor I'm trying to create my own swing component, but when I try to draw the border using drawRect, it only draws the left and top edges! Why is this happening and how to fix it? Here is my paintComponent method: @Override protected void paintComponent(Graphics

Can't Graphics.drawRect fully draw a rectangle?

mor I'm trying to create my own swing component, but when I try to draw the border using drawRect, it only draws the left and top edges! Why is this happening and how to fix it? Here is my paintComponent method: @Override protected void paintComponent(Graphics

Draw rectangle with animation with drawrect method?

Mahavir I have the following code in drawrect method. I want to draw with animation. -(void)drawRect:(CGRect)rect{ CGContextRef context = UIGraphicsGetCurrentContext(); CGMutablePathRef pathRef = CGPathCreateMutable(); CGPathMoveToPoint(pathRef, NULL, a.x,

Draw rectangle with animation with drawrect method?

Mahavir I have the following code in drawrect method. I want to draw with animation. -(void)drawRect:(CGRect)rect{ CGContextRef context = UIGraphicsGetCurrentContext(); CGMutablePathRef pathRef = CGPathCreateMutable(); CGPathMoveToPoint(pathRef, NULL, a.x,

Tkinter won't draw rectangle on Canvas

Cadel Watson I'm trying to initialize a grid of blue rectangles with a user-specified size. However, the rectangle is not drawn on the initialized canvas. I am trying to store them in a matrix for later use. My code is as follows: import Tkinter import sys fro

Tkinter won't draw rectangle on Canvas

Cadel Watson I'm trying to initialize a grid of blue rectangles with a user-specified size. However, the rectangle is not drawn on the initialized canvas. I am trying to store them in a matrix for later use. My code is as follows: import Tkinter import sys fro

Why doesn't HTML5 drawImage draw image on canvas?

humble 7 I tried adding an eventListener to draw the image only after the image is loaded. I've also tried putting event listeners before and after setting the image src, but still nothing seems to work. I can't seem to figure out what's causing it. Can someon

Why doesn't HTML5 drawImage draw image on canvas?

humble 7 I tried adding an eventListener to draw the image only after the image is loaded. I've also tried putting event listeners before and after setting the image src, but still nothing seems to work. I can't seem to figure out what's causing it. Can someon

Why doesn't HTML5 drawImage draw image on canvas?

humble 7 I tried adding an eventListener to draw the image only after the image is loaded. I've also tried putting event listeners before and after setting the image src, but still nothing seems to work. I can't seem to figure out what's causing it. Can someon

Why doesn't HTML5 drawImage draw image on canvas?

humble 7 I tried adding an eventListener to draw the image only after the image is loaded. I've also tried putting event listeners before and after setting the image src, but still nothing seems to work. I can't seem to figure out what's causing it. Can someon

Why doesn't HTML5 drawImage draw image on canvas?

humble 7 I tried adding an eventListener to draw the image only after the image is loaded. I've also tried putting event listeners before and after setting the image src, but still nothing seems to work. I can't seem to figure out what's causing it. Can someon

Why doesn't HTML5 drawImage draw image on canvas?

humble 7 I tried adding an eventListener to draw the image only after the image is loaded. I've also tried putting event listeners before and after setting the image src, but still nothing seems to work. I can't seem to figure out what's causing it. Can someon

HTML5 Canvas - how to draw rectangle on image on canvas

Lani Dolby Actually, I can do this using img.onloadfunctions . What I get from HTML5 canvas - how to draw lines on image background? '. But I need to draw the image without using the imgfrom onloadfunction like this: var imagePaper = new Image();

HTML5 Canvas - how to draw rectangle on image on canvas

Lani Dolby Actually, I can do this using img.onloadfunctions . What I get from HTML5 canvas - how to draw lines on image background? '. But I need to draw the image without using the imgfrom onloadfunction like this: var imagePaper = new Image();

HTML5 Canvas - how to draw rectangle on image on canvas

Lani Dolby Actually, I can do this using img.onloadfunctions . What I get from HTML5 canvas - how to draw lines on image background? '. But I need to draw the image without using the imgfrom onloadfunction like this: var imagePaper = new Image();

HTML5 Canvas - how to draw rectangle on image on canvas

Lani Dolby Actually, I can do this using img.onloadfunctions . What I get from HTML5 canvas - how to draw lines on image background? '. But I need to draw the image without using the imgfrom onloadfunction like this: var imagePaper = new Image();

Canvas doesn't draw text?

code M I want to display some text on the screen when the user clicks buttonwith canvasin android studio. I've been watching a lot of posts and videos on how to do this. The problem is, when I try their code, when I click the button, nothing is displayed. Does

Canvas doesn't draw in a loop

Saba Silagadze Why draw arent rectangles in a loop? I just started a snake game n trying to draw the snake's body through a loop, but for some reason ctx is not drawing the loop. If I write the rectangle without the loop, it works. var canv = document.getEleme