Draw transparent circle filled outside

后端 未结 3 1332
予麋鹿
予麋鹿 2020-12-01 17:12

I have a mapview that I want to draw a circle on to focus on a given area. But i want the circle to be inverted. That is, instead of the inside of the circle being filled, i

3条回答
  •  情深已故
    2020-12-01 18:15

    You can create a new BufferedImage, fill it grey then erase the circle where you want.

    And then, draw that BufferedImage on top of your view.

    BufferedImage img = new BufferedImage(sizeX, sizeY, BufferedImage.TYPE_INT_RGBA);
    Graphics2D g = img.createGraphics();
    
    int ovalX = 50;
    int ovalY = 70;
    int ovalRadius = 20;
    
    /* Draw the grey rectangle */
    g.setColor(Color.GRAY);
    g.fillRect(0, 0, sizeX, sizeY);
    
    /* Enable Anti-Alias */
    g.setRenderingHint(RenderingHints.HINT_ANTIALIAS, RenderingHints.VALUE_ANTIALIAS_ON);
    
    /* Clear the circle away */
    g.setComposite(AlphaComposite.CLEAR, 1.0f);
    g.fillOval(ovalX - ovalRadius, ovalY - ovalRadius, 2 * ovalRadius, 2 * ovalRadius);
    
    g.dispose();
    

提交回复
热议问题