libgdx修改图片资源为圆角矩形边框

modify picture resource to rounded rectangle border in libgdx

修改前

修改后

正如我们所见,我想通过code.I实现它已经搜索了很多资料,但他们只是用自己的颜色制作一个圆角矩形,而不是形成一个picture.Any帮助将不胜感激。

我为此制作了一个方法,它采用原始像素图(图像)并将其与蒙版结合,以将原始图像裁剪为蒙版的形状。我在这里的问题中发布了代码:

在你的情况下,遮罩应该是一个圆角矩形。只要 alpha 通道为“1”,蒙版的颜色是什么并不重要。然后结果将是您的原始图像裁剪到圆角矩形。

使用这个:

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);

            }
        }
    }
}