【问题标题】:modify picture resource to rounded rectangle border in libgdx将图片资源修改为libgdx中的圆角矩形边框
【发布时间】:2020-09-28 05:26:27
【问题描述】:

修改前

修改后

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

【问题讨论】:

  • 你的代码在哪里?

标签: libgdx rounded-corners


【解决方案1】:

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

libGDX: How can I crop Texture as a circle

在您的情况下,掩码应该是圆角矩形。只要 alpha 通道为“1”,遮罩的颜色是什么都没有关系。然后结果将是您的原始图像裁剪为圆角矩形。

【讨论】:

    【解决方案2】:

    使用这个:

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

    【讨论】:

      猜你喜欢
      • 2016-07-25
      • 2011-02-03
      • 1970-01-01
      • 2016-09-28
      • 2018-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多