我正在尝试在我的 android 应用程序中将可变位图中的像素区域设置为不同的颜色。不幸的是,我无法让 setPixels() 正常工作。我不断收到 ArrayOutOfBoundsExceptions。我认为这可能与步幅有关,但我真的不确定。这是我仍然不明白的唯一参数。我在 setPixels(不是 setPixel)上看到的唯一其他帖子是:drawBitmap() 和 setPixels():步幅是多少?它没有帮助我。我尝试将步幅设置为 0,作为位图的宽度,作为位图的宽度——我试图绘制的区域,它仍然崩溃。这是我的代码:
public void updateBitmap(byte[] buf, int offset, int x, int y, int width, int height) {
// transform byte[] to int[]
IntBuffer intBuf = ByteBuffer.wrap(buf).asIntBuffer();
int[] intarray = new int[intBuf.remaining()];
intBuf.get(intarray);
int stride = ??????
screenBitmap.setPixels(intarray, offset, stride, x, y, width, height); // crash here
我的位图是可变的,所以我知道这不是问题。我也确定我的字节数组正在正确转换为整数数组。但是我不断收到 ArrayOutOfBoundsExceptions ,我不明白为什么。请帮我解决这个问题
编辑 - 这是我构造假输入的方式:
int width = 1300;
int height = 700;
byte[] buf = new byte[width * height * 4 * 4]; // adding another * 4 here seems to work... why?
for (int i = 0; i < width * height * 4 * 4; i+=4) {
buf[i] = (byte)255;
buf[i + 1] = 3;
buf[i + 2] = (byte)255;
buf[i + 3] = 3;
}
//(byte[] buf, int offset, int x, int y, int width, int height) - for reference
siv.updateBitmap(buf, 0, 0, 0, width, height);
所以宽度和高度是正确的整数数量(至少应该是)。
EDIT2 - 这是screenBitmap的原始创建代码:
public Bitmap createABitmap() {
int w = 1366;
int h = 766;
byte[] buf = new byte[h * w * 4];
for (int i = 0; i < h * w * 4;i+=4) {
buf[i] = (byte)255;
buf[i+1] = (byte)255;
buf[i+2] = 0;
buf[i+3] = 0;
}
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
IntBuffer intBuf = ByteBuffer.wrap(buf).asIntBuffer();
int[] intarray = new int[intBuf.remaining()];
intBuf.get(intarray);
Bitmap bmp = Bitmap.createBitmap(metrics, w, h, itmap.Config.valueOf("ARGB_8888"));
bmp.setPixels(intarray, 0, w, 0, 0, w, h);
return bmp;
}
它似乎在这种情况下工作,不知道有什么区别