LibGdx中使用非2的N次幂尺寸的图片

Libgdx关于载入图片规格的问题

  对于libgdx来说,对载入的图片要求是:宽高都必须是2的N次幂的图片才行,否则会提示:texture width and height must be powers of two。

那么,如何使用非2的N次幂尺寸的图片呢,,使用以下函数:

import com.badlogic.gdx.graphics.Pixmap;import com.badlogic.gdx.graphics.Pixmap.Format;import com.badlogic.gdx.graphics.Texture;

// 如工程目录assets/texture/001.png下: filePath = "texture/001.png"/** 载入任意尺寸的图像,创建Texture */public static Texture loadTexture(String filePath){Pixmap pic = new Pixmap(Gdx.files.internal(filePath));pic = Format2power(pic);// 图像尺寸转化为2的幂return new Texture(pic);// 从Pixmap构建Texture}/** 将Pic转化为尺寸为2的幂的图像 */public static Pixmap Format2power(Pixmap Pic){int width = Pic.getWidth(), height = Pic.getHeight();// 获取图像的尺寸Pixmap image = new Pixmap(closestTwoPower(width), closestTwoPower(height), Format.RGBA8888);if (image.getWidth() == width && image.getHeight() == height) return image;// 若原图像尺寸为2的幂,则直接返回原图像int color;for (int i = 0; i < width; i++){for (int j = 0; j < height; j++){color = Pic.getPixel(i, j);// 原图像像素信息image.drawPixel(i, j, color);// 生成图像}}return image;}/** 获取最接近于n的2的幂 */public static int closestTwoPower(int n){int power = 1;while (power < n)power <<= 1;return power;}

失败是成功的亲娘,没有失败哪来的成功呢?诺贝尔如果不经历千万次的失败,

LibGdx中使用非2的N次幂尺寸的图片

相关文章:

你感兴趣的文章:

标签云: