与Config.ALPHA_8像素完美碰撞 [英] Pixel perfect collision with Config.ALPHA_8

查看:150
本文介绍了与Config.ALPHA_8像素完美碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个$ C $的像素完美碰撞检测c和我的code使用它:

I've found this code of pixel perfect collision checking and used it in my code:

public  boolean isCollisionDetected(Bitmap bitmap1, int x1, int y1,
        Bitmap bitmap2, int x2, int y2) {
    Rect bounds1 = new Rect(x1, y1, x1 + bitmap1.getWidth(), y1
            + bitmap1.getHeight());
    Rect bounds2 = new Rect(x2, y2, x2 + bitmap2.getWidth(), y2
            + bitmap2.getHeight());

    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++) {
                int bitmap1Pixel = bitmap1.getPixel(i - x1, j - y1);
                int bitmap2Pixel = bitmap2.getPixel(i - x2, j - y2);
                if (isFilled(bitmap1Pixel) && isFilled(bitmap2Pixel)) {
                    return true;
                }
            }
        }
    }
    return false;
}

private  Rect getCollisionBounds(Rect rect1, Rect rect2) {
    int left = (int) Math.max(rect1.left, rect2.left);
    int top = (int) Math.max(rect1.top, rect2.top);
    int right = (int) Math.min(rect1.right, rect2.right);
    int bottom = (int) Math.min(rect1.bottom, rect2.bottom);
    return new Rect(left, top, right, bottom);
}

private  boolean isFilled(int pixel) {
    return pixel != Color.TRANSPARENT;
}

和它的工作完美,没有任何问题,什么那么。这直到我使用(因为内存问题),这code将图像转换成Config_Alpha_8:

And it worked perfectly, no problems what so ever. That until I set the images into Config_Alpha_8 using this code (because of ram problems):

    private Bitmap convert(Bitmap bitmap, Bitmap.Config config) {
    Bitmap convertedBitmap = 
                  Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), config);
    Canvas canvas = new Canvas(convertedBitmap);
    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    return convertedBitmap;
}

为什么没有什么不会发生,但是它里面去isCollisioDetected,我已经与日志检查!但它不会碰撞检测..谁能提供工作功能检查位图的像素完美碰撞与config_alpha_8?

Why Nothing wont happen, it does go inside isCollisioDetected, I've checked with logs! but it wont detect collision.. can anyone provide working function for checking pixel perfect collision of bitmap with config_alpha_8?

推荐答案

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

Calling getPixel() on a bitmap with ALPHA_8 configuration will always return zero. This seems to be a bug

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

You can work around this problem by storing the pixels of each bitmap as a byte array:

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

您将需要修改你的碰撞检测功能一点:

You will need to modify your collision detection function a little:

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

(......你可能要更改的位图参数到这些位图的相应的宽度和高度,并调用循环()。)

(... you might want to change the bitmap parameters into the respective width and height of the those bitmaps and call recycle().)

这篇关于与Config.ALPHA_8像素完美碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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