12

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

4

2 回答 2

9

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

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;
}
于 2013-11-02T20:53:42.710 回答
1

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

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);
}
于 2017-06-07T04:11:39.873 回答