栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

与Config.ALPHA_8的像素完美碰撞

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

与Config.ALPHA_8的像素完美碰撞

在具有ALPHA_8配置的位图上调用getPixel()将始终返回零。这似乎是一个错误

您可以通过将每个位图的像素存储为字节数组来解决此问题:

byte[] pixelData = getPixels(convert(bitmap, Bitmap.Config.ALPHA_8));... public byte[] getPixels(Bitmap bmp) {    int bytes = bmp.getRowBytes() * bmp.getHeight();    ByteBuffer buffer = ByteBuffer.allocate(bytes);    bmp.copyPixelsToBuffer(buffer);    return buffer.array();}

您将需要稍微修改碰撞检测功能:

public  boolean isCollisionDetected(byte[] pixels1, Bitmap bitmap1, int x1, int y1, byte[] pixels2, Bitmap bitmap2, int x2, int y2) {        int width1 =bitmap1.getWidth();        int height1=bitmap1.getHeight();        int width2 =bitmap2.getWidth();        int height2=bitmap2.getHeight();        Rect bounds1 = new Rect(x1, y1, x1 + width1, y1 + height1);        Rect bounds2 = new Rect(x2, y2, x2 + width2, y2 + height2);        if (Rect.intersects(bounds1, bounds2)) { Rect collisionBounds = getCollisionBounds(bounds1, bounds2); for (int i = collisionBounds.left; i < collisionBounds.right; i++) {     for (int j = collisionBounds.top; j < collisionBounds.bottom; j++) {         byte bitmap1Pixel = pixels1[((j - y1) * width1) + (i - x1)];         byte bitmap2Pixel = pixels2[((j - y2) * width2) + (i - x2)];         if (isFilled(bitmap1Pixel) && isFilled(bitmap2Pixel)) {  return true;         }     } }        }        return false;    }

(…您可能希望将位图参数更改为这些位图的相应宽度和高度,然后调用recycle()。)



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/451600.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号