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

时间:2016-06-09 03:05:02

标签: libgdx rounded-corners

修改之前

Before modifing

之后

after modyfing

正如我们所看到的,我想通过代码实现它。我搜索了许多材料,但它们只是按照自己的颜色制作圆角矩形,而不是形成一张图片。任何帮助都将非常感激。

2 个答案:

答案 0 :(得分:0)

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

libGDX: How can I crop Texture as a circle

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

答案 1 :(得分:0)

使用此:

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

            }
        }
    }
}