有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在libgdx中翻转pixmap以绘制纹理?

所以我要做的是通过在纹理上绘制像素贴图来为我的游戏生成背景图像。到目前为止,我可以做到这一点,但现在我需要绘制在X或Y轴翻转到纹理的像素贴图。但是我找不到任何可以这样做的东西。pixmap类不提供这种功能。然后我想我可以画一个翻转的纹理区域到一个纹理,但到目前为止我还没有找到如何做到这一点。所以我想知道我该如何做这样的事情,是否可以用其他java库翻转png图像,然后从翻转的图像创建一个pixmap


共 (2) 个答案

  1. # 1 楼答案

    这里有一个解决方案,不需要创建新的Pixmap。还可以修改此代码,通过交换Pixmap图像的角点,而不是交换图像两侧的像素,来水平和垂直翻转Pixmap

    public static void flipPixmap( Pixmap p ){
        int w = p.getWidth();
        int h = p.getHeight();
        int hold;
    
        //change blending to 'none' so that alpha areas will not show
          //previous orientation of image
        p.setBlending(Pixmap.Blending.None);
        for (int y = 0; y < h / 2; y++) {
            for (int x = 0; x < w / 2; x++) {
                //get color of current pixel
                hold = p.getPixel(x,y);
                //draw color of pixel from opposite side of pixmap to current position
                p.drawPixel(x,y, p.getPixel(w-x-1, y));
                //draw saved color to other side of pixmap
                p.drawPixel(w-x-1,y, hold);
                //repeat for height/width inverted pixels
                hold = p.getPixel(x, h-y-1);
                p.drawPixel(x,h-y-1, p.getPixel(w-x-1,h-y-1));
                p.drawPixel(w-x-1,h-y-1, hold);
            }
        }
        //set blending back to default
        p.setBlending(Pixmap.Blending.SourceOver);
    }
    
  2. # 2 楼答案

    除了迭代像素,我也看不到其他选项:

    public Pixmap flipPixmap(Pixmap src) {
        final int width = src.getWidth();
        final int height = src.getHeight();
        Pixmap flipped = new Pixmap(width, height, src.getFormat());
    
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                flipped.drawPixel(x, y, src.getPixel(width - x - 1, y));
            }
        }
        return flipped;
    }