安卓的setPixels()说明和实例? [英] Android SetPixels() Explanation and Example?

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

问题描述

我想不同的颜色在我的Andr​​oid应用程序中设置像素的区域的可变位。不幸的是,我不能得到的setPixels()才能正常工作。我经常收到ArrayOutOfBoundsExceptions。我认为它可能有一些做的步伐,但我真的不知道。这是我还是不明白的唯一参数。我看到的的setPixels(不setPixel)唯一的其他职位是在这里:<一href="http://stackoverflow.com/questions/4797411/drawbitmap-and-setpixels-whats-the-stride">drawBitmap()和的setPixels():什么是步幅,并没有帮助我。我尝试设置步幅为0,为位图的宽度,作为位图的宽度 - 这片区域我想要绘制和它仍然崩溃。这是我的code:

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 - 这里是$ C $下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);

因为你已经转换字节为int。你的错误是ArrayOutOfBoundsExceptions。检查是否大小 intBuf.remaining()=宽*高/ 4

这篇关于安卓的setPixels()说明和实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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