Android SetPixels() 解释和例子? [英] Android SetPixels() Explanation and Example?

查看:20
本文介绍了Android SetPixels() 解释和例子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的 android 应用程序中将可变位图中的像素区域设置为不同的颜色.不幸的是,我无法让 setPixels() 正常工作.我不断收到 ArrayOutOfBoundsExceptions.我认为这可能与步幅有关,但我真的不确定.这是我仍然不明白的唯一参数.我在 setPixels(不是 setPixel)上看到的唯一其他帖子在这里:drawBitmap() 和setPixels():步幅是多少? 但它对我没有帮助.我尝试将步幅设置为 0,作为位图的宽度,作为位图的宽度 - 我正在尝试绘制的区域,但它仍然崩溃.这是我的代码:

I am trying to set a region of pixels in a mutable bitmap a different color in my android app. Unfortunately I cannot get setPixels() to work properly. I am constantly getting ArrayOutOfBoundsExceptions. I think it may have something to do with the stride, but I'm really not sure. That is the only parameter that I still don't understand. The only other post I have seen on setPixels (not setPixel) is here: drawBitmap() and setPixels(): what's the stride? and it did not help me. I tried setting the stride as 0, as the width of the bitmap, as the width of the bitmap - the area i'm trying to draw and it still crashes. Here is my code:

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,我不明白为什么.请帮我解决这个问题

My bitmap is mutable, so I know that is not the problem. I am also certain that my byte array is being properly converted to an integer array. But I keep getting ArrayOutOfBoundsExceptions and I don't understand why. Please help me figure this out

编辑 -这是我构造假输入的方法:

EDIT - here is how I construct the fake input:

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

所以宽度和高度是正确的整数数量(至少应该是).

So the width and height are the correct amount of ints (at least it should be).

EDIT2 - 这里是 screenBitmap 的原始创建代码:

EDIT2 - here is the code for the original creation of 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;
}

在这种情况下似乎有效,不知道有什么区别

It seems to work in this instance, not sure what the difference is

推荐答案

大概应该是:

screenBitmap.setPixels(intarray, 0, width / 4, x, y, width / 4, height);

因为您已将字节转换为整数.你的错误是 ArrayOutOfBoundsExceptions.检查尺寸是否intBuf.remaining() = width * height/4.

because you have converted byte to int. your error is ArrayOutOfBoundsExceptions. check whether the size intBuf.remaining() = width * height / 4.

这篇关于Android SetPixels() 解释和例子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆