2

Before modifing

Before modifing

after modyfing

after modyfing

just as we can see,i want to achieve it by code.I have searched many materials ,but they are just making a rounded rectangle by their own color,not form one picture.Any help will be very appreciated.

Gangaraju
  • 4,406
  • 9
  • 45
  • 77
Jing Hu
  • 67
  • 8

2 Answers2

0

I made a method for doing this which takes an original pixmap (image) and combines it with a mask to crop the original image to the shape of the mask. I posted the code in a question here:

libGDX: How can I crop Texture as a circle

In your case the mask should be a rounded rectangle. It doesn't matter what the color of the mask is as long as the alpha channel is "1". Then the result will be your original image cropped to the rounded rectangle.

Community
  • 1
  • 1
Tekkerue
  • 1,497
  • 1
  • 11
  • 17
0

Use this:

public class RoundImage extends Image {

    private int radius;
    private int diameter;
    public RoundImage(int radius) {
        this.radius = radius;
        this.diameter = radius * 2;
    }
    @Override public void setDrawable (Drawable drawable) {
        if(drawable instanceof TextureRegionDrawable)
        {
            Pixmap origPixmap = ((TextureRegionDrawable) drawable).getRegion().getTexture().getTextureData().consumePixmap();
            Pixmap pixmap = round(origPixmap);
            drawable = new TextureRegionDrawable(new TextureRegion(new Texture(pixmap)));
//            origPixmap.dispose();
        }
        super.setDrawable(drawable);
    }

    private Pixmap round(Pixmap pixmap)
    {
        int width = pixmap.getWidth();
        int height = pixmap.getHeight();
        int min = Math.min(width,height);
        int max = Math.max(width,height);
        Pixmap round =  new Pixmap(width ,height, Pixmap.Format.RGBA8888);
        round.drawPixmap(pixmap , radius , 0 , radius , 0 , pixmap.getWidth() - diameter , pixmap.getHeight());
        round.drawPixmap(pixmap , 0 , radius , 0 , radius , radius , pixmap.getHeight() - diameter);
        round.drawPixmap(pixmap , pixmap.getWidth() - radius , radius , pixmap.getWidth() - radius , radius , radius , pixmap.getHeight() - diameter);
        //---------------- draw rounds
        draw_top_left_round(round, pixmap);
        draw_top_right_round(round, pixmap);
        draw_bottom_right_round(round, pixmap);
        draw_bottom_left_round(round, pixmap);

        return round;
    }
    private void draw_bottom_right_round(Pixmap round, Pixmap pixmap) {
        draw_round(round , pixmap , pixmap.getWidth() - diameter , pixmap.getHeight() - diameter);
    }
    private void draw_bottom_left_round(Pixmap round, Pixmap pixmap) {
        draw_round(round , pixmap , 0 , pixmap.getHeight() - diameter);
    }
    private void draw_top_left_round(Pixmap round, Pixmap pixmap) {
        draw_round(round,pixmap, 0,0);
    }
    private void draw_top_right_round(Pixmap round, Pixmap pixmap) {
        draw_round(round , pixmap, pixmap.getWidth() - diameter,0);
    }
    //--------------------------
    private void draw_round(Pixmap round, Pixmap pixmap , int x_offsetStart , int y_offsetStart) {
        for(int y = y_offsetStart; y < y_offsetStart + diameter; y++)
        {
            for(int x = x_offsetStart; x < x_offsetStart + diameter ; x++)
            {
                double dist_x = (radius - x + x_offsetStart);
                double dist_y = radius - y + y_offsetStart;
                double dist = Math.sqrt((dist_x * dist_x) + (dist_y * dist_y));
                if(dist < radius)
                {
                    round.drawPixel(x, y,pixmap.getPixel(x, y));
                }
                else
                    round.drawPixel(x, y,0);

            }
        }
    }
}
ultra.deep
  • 1,699
  • 1
  • 19
  • 23