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

在libgdx中将图片资源修改为圆角矩形边框,libgdx,rounded-corners,Libgdx,Rounded Corners,修改前 改后 正如我们所看到的,我想通过代码来实现它。我搜索了很多材料,但他们只是根据自己的颜色制作一个圆角矩形,而不是形成一张图片。任何帮助都将非常感谢。我制作了一种方法,它采用原始的pixmap(图像)并将其与遮罩组合,以将原始图像裁剪为遮罩的形状。我在这里的一个问题中发布了代码: 在您的情况下,遮罩应该是一个圆角矩形。只要alpha通道为“1”,遮罩的颜色就不重要。然后,结果将是将原始图像裁剪为圆角矩形。使用以下方法: public class RoundImage extends

修改前

改后


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

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

在您的情况下,遮罩应该是一个圆角矩形。只要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);

            }
        }
    }
}
public类RoundImage扩展了Image{
私有整数半径;
私有内径;
公共圆形图像(整数半径){
这个半径=半径;
该直径=半径*2;
}
@覆盖公共void setDrawable(Drawable Drawable){
if(纹理区域的可绘制实例)
{
Pixmap origPixmap=((TextureRegionRavable)drawable).getRegion().getTexture().getTextureData().consumePixmap();
Pixmap Pixmap=圆形(origPixmap);
可绘制=新纹理区域可绘制(新纹理区域(新纹理(pixmap));
//dispose();
}
超级可设置可拉伸(可拉伸);
}
私人Pixmap回合(Pixmap Pixmap)
{
int width=pixmap.getWidth();
int height=pixmap.getHeight();
int min=数学最小值(宽度、高度);
int max=数学最大值(宽度、高度);
Pixmap round=新的Pixmap(宽度、高度、Pixmap.Format.rgba888);
round.drawPixmap(pixmap,radius,0,radius,0,pixmap.getWidth()-diameter,pixmap.getHeight());
round.drawPixmap(pixmap,0,radius,0,radius,radius,pixmap.getHeight()-直径);
round.drawPixmap(pixmap,pixmap.getWidth()-半径,半径,pixmap.getWidth()-半径,半径,pixmap.getHeight()-直径);
//----------------抽签
绘制上下圆(圆,像素地图);
绘制右上方的圆形(圆形,像素贴图);
绘制底部\右侧\圆形(圆形,像素贴图);
绘制底部左圆形(圆形,像素地图);
回程;
}
私有空白绘制\底部\右侧\圆形(Pixmap圆形,Pixmap Pixmap){
画圆(圆,pixmap,pixmap.getWidth()-直径,pixmap.getHeight()-直径);
}
私有空白绘制\底部\左侧\圆形(Pixmap圆形,Pixmap Pixmap){
画圆(圆,pixmap,0,pixmap.getHeight()-直径);
}
私有空位绘制(顶部左圆形)(Pixmap圆形,Pixmap Pixmap){
画圆(圆,像素图,0,0);
}
私有无效绘制\u顶部\u右侧\u圆形(Pixmap圆形,Pixmap Pixmap){
画圆(圆,pixmap,pixmap.getWidth()-直径,0);
}
//--------------------------
专用无效取回轮(取回轮、取回轮、取回轮、取回轮、取回轮){
对于(int y=y_offsetStart;y
您的代码在哪里?